Repository navigation
Conversation
Down recursion over relations (>> always, and > when the input has
relations) collected the relations' node members and the ways' nodes
in two separate passes over the node blocks, then took the union.
Blocks holding both kinds of node were read and decompressed twice.
Add relation_and_way_node_members, which merges the two sorted id
lists and the two range sets and reads the node blocks once. Use it
for >> and for > on current data without a range restriction. The
history path and localize's restricted-range path are unchanged. The
result is the union of the two old results, as before.
Nova Scotia extract, CPU time, alternating runs:
rel[boundary=administrative][admin_level=4][name="Nova Scotia"];
(._;>>;);
714 ms -> 559 ms; node block reads 2570 -> 1490 (1489 distinct)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
|
Colin's thoughts: high confidence, medium impact, low risk. This is about doing more with fewer reads. The fast path only applies for current and non-local queries. Followup: expand this to attic and local queries if the expanded fuzzer shows it's a hotspot, too (or if knowledge of prod query mix suggests that.) |
|
Follow-up (attic part): Generated by Claude Code |
Fixes #16.
Change
Down recursion over relations collected the relations' node members and the ways' nodes in two separate passes over the node blocks, then took the union. That applies to
>>always, and to>when the input contains relations. Blocks holding both kinds of node were read and decompressed twice.This adds
relation_and_way_node_members(data/collect_members.cc). It merges the two sorted id lists and the two index range sets, then reads the node blocks once. It replaces the two passes in:>>(RECURSE_DOWN_RELinstatements/recurse.cc)>on current data without a range restriction (add_nw_member_objects)Each node is still found by its id within the ranges computed for it, so the result is the same union as before.
The two call sites differ in shape:
>>: the old two-pass code is replaced outright.add_nw_member_objects(>): the new code is a fast path at the top of the function that returns early. The old two-pass code below it stays, because it still serves two callers:[date:]), through itselsebranchlocal(statements/localize.cc), which passes a range restriction (ranges_32_). The old code applies that restriction to the relations' nodes but not to the ways' nodes, an asymmetry a single merged pass can't easily reproduce.Folding those two cases in too would make the change larger to review.
Three files, about 50 lines. Based on fossgis
master. No change to the database format or the query API.Why not a block cache
#16 proposed caching decompressed blocks within a query. Simulating an LRU cache on the real block-read order ruled that out. Benchmark q07 (below) reads the same blocks in two long passes, so it only benefits from a cache of 256 MB or more per query process; at 16–128 MB its hit rate is 2%. Removing the second pass fixes the cause, with no memory cost.
Impact
CPU time (user + system), median of 5 alternating runs per build (15 for the hiking-route queries), Nova Scotia test database (Geofabrik extract of 2026-11-08, imported with
--meta), warm cache, 4-core VM. Output is byte-identical for every query.masterrel[boundary=administrative][admin_level=4][name="Nova Scotia"]; (._;>>;); out count;(benchmark q07)rel[type=multipolygon](44.60,-63.66,44.70,-63.52); (._;>>;); out count;rel(44.60,-63.66,44.70,-63.52); (._;>;); out count;rel[route=hiking]; (._;>>;); out count;rel[route=hiking]; (._;>;); out count;way[highway=motorway]; (._;>;); out count;(benchmark q05: ways only, nothing read twice)(All queries start with
[out:csv(::count)];.)For q07, node block reads drop from 2,570 to 1,490, with 1,489 distinct blocks (measured by tracing block
mmapcalls).Testing
master(bench/fuzz.py, see Benchmark queries and differential fuzzer #2). About 1,040 use>or>>, up to about 600 of them with inputs that can contain relations. 0 differences; 12 skipped, all of which time out onmaster.complete). That covered 10,000 queries across the plain, area and history databases (6,000 / 2,000 / 2,000), with 0 differences. The per-PR fuzz run above predates a fuzzer fix: about 8% of its queries were invalid on both builds, so it rests on roughly 5,500 queries that ran (see Benchmark queries and differential fuzzer #2).Memory
Peak memory (RSS) rises by 0.1–1.0 MB (q07: 34.1 → 35.1 MB), from holding the merged id list. One result set is built instead of two.
Limitations
[date:]queries, andlocal, keep the two-pass code; the test database has no history, so that path couldn't be checked anyway.Generated by Claude Code