Skip to content

Reject non-matching ids with a bit filter before binary search - #1

Open
cldellow wants to merge 1 commit into
masterfrom
faster-id-predicate
Open

cldellow wants to merge 1 commit into
masterfrom
faster-id-predicate

Conversation

@cldellow

@cldellow cldellow commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Fixes #6.

Change

Id_Predicate checks each candidate object with std::binary_search over a sorted id list. Most candidates are not in the list: a tag query such as nwr[amenity=restaurant] gets the matching ids from the tag index, then decompresses every node block of each quadtile holding one of them and tests every node in those blocks.

This adds a small hashed bit filter in front of the binary search:

  • The filter rejects most absent ids with one bit test. The binary search still decides every id the filter lets through, so results are unchanged.
  • It uses about 16 bits per id, capped at 16 MiB.
  • Collect_Items builds its predicate once instead of once per chunk.

There is no change to the database format or the query API. Two files changed, about 60 lines.

Impact

Median CPU time (user + system) of osm3s_query, 7 runs per build, with warm page cache. The data is the Geofabrik Nova Scotia extract of 2026-11-08 (12.6M nodes, 882k ways, 20.6k relations), imported with --meta. The baseline is master (8edf11b). Output is byte-identical to the baseline for every query.

The queries and the harness (bench/compare.py) are on the bench-tools branch; the queries are described in #2.

Default compression (LZ4 on .bin files, .map files uncompressed):

Query Baseline This PR Change
q01 nwr[amenity=restaurant] 160 ms 124 ms −22%
q02 node[natural=peak] 183 ms 129 ms −30%
q05 motorways (._;>;) 140 ms 119 ms −15%
q07 province boundary (._;>>;) 682 ms 571 ms −16%
q03, q04, q06, q08, q09, q11 −2% to −11%
q10 highways out geom 470 ms 486 ms +3% (within noise)
All 11 queries 2432 ms 2157 ms −11%

With nodes.bin and ways.bin uncompressed (all other files LZ4): decompression drops out, so the id check is a larger share of what is left.

Query Baseline This PR Change
q01 75 ms 39 ms −48%
q02 95 ms 44 ms −54%
q05 62 ms 46 ms −25%
q07 423 ms 300 ms −29%
All 11 queries 1760 ms 1511 ms −14%

The queries that hardly use the id check (bbox scans, out geom, out meta, around) change by 6% or less, and they dominate the totals.

Profile of q01 on the LZ4 database: binary_search falls from 36% of samples to 4%, while the filter adds about 7%. LZ4 decompression rises from 43% to 66% and is now the main cost.

Benchmark queries (from bench/queries/ on the bench-tools branch)
Name Query
q01_global_tag [out:csv(::count)]; nwr[amenity=restaurant]; out count;
q02_global_rare_tag [out:csv(::count)]; node[natural=peak]; out count;
q03_bbox_all_nodes [out:csv(::count)]; node(44.60,-63.66,44.70,-63.52); out count;
q04_bbox_tag [out:csv(::count)]; way[highway](44.60,-63.66,44.70,-63.52); out count;
q05_recurse_down [out:csv(::count)]; way[highway=motorway]; (._;>;); out count;
q06_around [out:csv(::count)]; node[amenity=cafe](44.60,-63.66,44.70,-63.52); node(around:300)[shop]; out count;
q07_big_relation [out:csv(::count)]; rel[boundary=administrative][admin_level=4][name="Nova Scotia"]; (._;>>;); out count;
q08_out_meta [out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta;
q09_out_geom_buildings [out:json]; way[building](44.640,-63.585,44.655,-63.565); out geom;
q10_out_geom_highways [out:json]; way[highway](44.60,-63.66,44.70,-63.52); out geom;
q11_recurse_up [out:csv(::count)]; node[highway=traffic_signals](44.60,-63.66,44.70,-63.52); way(bn); rel(bw); out count;

Bounding boxes are in Halifax, Nova Scotia; q07 names the Nova Scotia province boundary.

Testing

  • Differential fuzzer: 3,000 queries against master (bench/fuzz.py, seed 2; see Benchmark queries and differential fuzzer #2). 2,997 identical, 3 skipped (timeouts), 0 differences.
  • Mutation test: a copy of this change with a deliberate bug (the id check drops ids divisible by 97, about 1%) differed on 262 of the same 3,000 queries. So the fuzzer catches errors in this code, including indirect ones (around uses the id check to fetch way nodes).
  • 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 first fuzz run above predates a fuzzer fix: about 8% of its queries were invalid on both builds (see Benchmark queries and differential fuzzer #2).

Memory

Bound: the filter uses about 2 bytes per id (16 bits), capped at 16 MiB per predicate, alongside the id list's own 8 bytes per id. Overpass counts only result sets toward [maxsize:]. Auxiliary structures, such as the id list this filter sits in front of or around's segment lists, are not counted, and these new structures follow that convention. Counting them would change which queries fail with "Oversized", which is an observable behaviour change.

Peak memory (RSS), Nova Scotia test database, master against this change combined with the others in #12:

Query master Combined
q01 (nwr[amenity=restaurant]) 7.9 MB 8.3 MB
q07 (Nova Scotia boundary, (._;>>;)) 33.6 MB 33.8 MB
2.8M node ids: [out:csv(::count)]; way[highway]->.w; node(w.w); out count; 170.2 MB 170.2 MB

The 2.8M-id query shows no change. It may not use Id_Predicate, or the filter's allocation may not coincide with the peak; that hasn't been traced.

Limitations and open questions

  • Production compression settings are unknown. The gain depends on them: about 22% on q01 with LZ4 and about 48% with uncompressed node and way files. Checking a production .idx header would tell us (od -An -tu2 -j6 -N2 nodes.bin.idx: 0 = none, 1 = zlib, 2 = LZ4).
  • The query mix is not representative. These are 11 hand-written queries. Production CPU is likely dominated by heavier queries, so a sample of real queries would show the actual effect.
  • Only one setup is tested: one regional extract, warm caches, one 4-core VM. Planet-scale id sets (millions of ids from large recursions) are untested.
  • The filter's memory is not counted against [maxsize:]. It is at most 16 MiB per predicate, and most queries use a few KB. A lower cap, or reporting the allocation to Resource_Manager, would close this gap.
  • The filter weakens past about 8M ids. Once the 16 MiB cap is reached, the false-positive rate rises (about 30% at 50M ids). Results stay correct but the speedup shrinks.
  • There may be a small cost when most candidates match. In some recursions most candidates are in the id list, so the filter adds one memory read per candidate. q10 is +3% here, which is within noise, but this case should be measured with a recursion-heavy query set.
  • There are no unit tests. The fork has none, and none are added here. The regression checks are the benchmark's output equality and the differential fuzzer (see Testing). A randomized test of the filter against binary_search would be a good addition.

https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT


Generated by Claude Code

Id_Predicate checks every object it is shown with std::binary_search over
the sorted id list. Most objects it sees are not in the list: a tag query
such as node[amenity=restaurant] finds the matching ids in the tag index,
then reads every node block of each quadtile that holds one of them and
tests each node against the ids.

Add a small hashed bit filter (about 16 bits per id, at most 16 MiB) that
rejects most absent ids with one bit test; the binary search still decides
every id the filter lets through, so results do not change. Collect_Items
now builds its predicate once instead of once per chunk.

On a Nova Scotia extract (12.6M nodes), median CPU time per query with
warm caches:
  nwr[amenity=restaurant]       160 ms -> 124 ms
  node[natural=peak]            183 ms -> 129 ms
  rel boundary with (._;>>;)    682 ms -> 571 ms
Outputs are byte-identical.

No change to the database format or the API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
@cldellow

Copy link
Copy Markdown
Owner Author

Colin's thoughts: low risk, high confidence, high benefit.

Use a lossy hashmap to bail early before paying for an expensive binary search.

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. query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Id checks dominate tag-lookup and recursion queries (binary search per candidate)

2 participants