Skip to content

Collect relation node members and way nodes in one pass - #18

Open
cldellow wants to merge 1 commit into
masterfrom
single-pass-member-nodes
Open

cldellow wants to merge 1 commit into
masterfrom
single-pass-member-nodes

Conversation

@cldellow

@cldellow cldellow commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

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_REL in statements/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:
    • history queries ([date:]), through its else branch
    • local (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.

Query master This PR Change
rel[boundary=administrative][admin_level=4][name="Nova Scotia"]; (._;>>;); out count; (benchmark q07) 714 ms 559 ms −22%
rel[type=multipolygon](44.60,-63.66,44.70,-63.52); (._;>>;); out count; 173 ms 150 ms −13%
rel(44.60,-63.66,44.70,-63.52); (._;>;); out count; 436 ms 388 ms −11%
rel[route=hiking]; (._;>>;); out count; 143 ms 131 ms −8%
rel[route=hiking]; (._;>;); out count; 134 ms 125 ms −7%
way[highway=motorway]; (._;>;); out count; (benchmark q05: ways only, nothing read twice) 143 ms 145 ms unchanged

(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 mmap calls).

Testing

  • Differential fuzzer, seeds 2 and 3: 6,000 queries against 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 on master.
  • Recursion queries above: identical output.
  • Update, re-fuzzed with the corrected fuzzer: this change has since been re-fuzzed as part of the all-changes branch (All changes combined (tracking PR) #12), with the corrected fuzzer and a bigger vocabulary (areas, history, users and 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

  • History queries still use two passes. [date:] queries, and local, keep the two-pass code; the test database has no history, so that path couldn't be checked anyway.
  • Narrow test setup: one regional extract, warm cache, one VM.

Generated by Claude Code

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
@cldellow cldellow added the query-perf Query performance change; collected on all-changes label Nov 21, 2026 — with Claude
@cldellow

Copy link
Copy Markdown
Owner Author

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.)

@cldellow cldellow added human-reviewed Colin has reviewed this. needs-followup Colin reviewed and wants Claude to iterate further, but didn't have spare agents available. labels Nov 21, 2026

Copy link
Copy Markdown
Owner Author

Follow-up (attic part): [date:] recursion turned out to be a hotspot. rel[route=hiking]; (._;>;); at a past date read every node block twice, and also read the node metadata twice. Extended to past dates in #95 (stacked on this PR): −37% for hiking routes and −25% for bus routes, with 0 differences on fuzzing plus a targeted check. local still uses two passes. It is rare in practice, and its range restriction applies only to the relations' nodes, so a single pass would need a separate design. I don't think it's worth it unless production logs show local is common.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

human-reviewed Colin has reviewed this. needs-followup Colin reviewed and wants Claude to iterate further, but didn't have spare agents available. query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The same data blocks are decompressed more than once per query

2 participants