Skip to content

Reject most non-matching ids before the binary search in Id_Predicate - #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

Change

Id_Predicate::match tests each candidate object with std::binary_search over the 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 reads every block of each quadtile that holds one of them and tests every object in those blocks, so most of the binary searches fail.

  • New Id_Bit_Filter: a hashed bit set of about 16 bits per id (rounded up to a power of two, at most 2^27 bits = 16 MiB). One bit test rejects most absent ids: about 3–6% of absent ids pass it below the cap.
  • Id_Predicate checks the filter first. The binary search still decides every id the filter lets through, so results can't change.
  • Collect_Items builds its Id_Predicate once, in its constructor, instead of once per get_chunk, so the filter isn't rebuilt for every chunk. The predicate holds the filter through a shared_ptr because predicates are copied by value (Not_Predicate, And_Predicate, function arguments).
  • The hash is Fibonacci hashing (Knuth, The Art of Computer Programming, Vol. 3, §6.4, multiplicative hashing): multiply the id by 2^64/φ (0x9E3779B97F4A7C15) and keep the top bits. OSM ids are clustered, and this spreads runs of nearby ids over the whole bit set. The constant is the one in Linux's hash_64 (GOLDEN_RATIO_64) and Boost's hash_combine. std::hash<uint64_t> is the identity in libstdc++ and libc++, so it doesn't help here.

No change to the database format or the query API. Two files, +54 −7.

Impact

Median CPU time (user + system) of osm3s_query over 7 alternating runs per build, warm page cache, 4-core 2.1 GHz Xeon VM. Data: Nova Scotia as of 2026-11-15 (12.7M nodes, 883k ways, 21.4k relations), imported with --meta and LZ4 compression. Baseline: master 555f744.

Query master This PR Change
[out:csv(::count)]; nwr[amenity=restaurant]; out count; 159 ms 128 ms −19%
[out:csv(::count)]; node[natural=peak]; out count; 179 ms 129 ms −28%
[out:csv(::count)]; way[highway=motorway]; (._;>;); out count; 145 ms 127 ms −12%
[out:csv(::count)]; rel[boundary=administrative][admin_level=4][name="Nova Scotia"]; (._;>>;); out count; 720 ms 588 ms −18%
[out:csv(::count)]; node[amenity]; way(bn); out count; 641 ms 513 ms −20%
[out:csv(::count)]; rel[route=hiking]; (._;>;); way(bn); out count; 225 ms 199 ms −11%
All 19 queries below 7,741 ms 6,991 ms −10%

Queries that rarely test ids (bbox scans, around, out geom, out meta) change by less than 8%. The three that came out slower in the table below (by 2–6%) were re-run as 25 alternating pairs. Their mean ratios are 1.008, 0.984 and 0.996, and every 95% confidence interval includes 1.0.

All 19 queries
Query master This PR Ratio
[out:csv(::count)]; nwr[amenity=restaurant]; out count; 159 ms 128 ms 0.81
[out:csv(::count)]; node[natural=peak]; out count; 179 ms 129 ms 0.72
[out:csv(::count)]; node(44.60,-63.66,44.70,-63.52); out count; 39 ms 40 ms 1.02
[out:csv(::count)]; way[highway](44.60,-63.66,44.70,-63.52); out count; 135 ms 124 ms 0.92
[out:csv(::count)]; way[highway=motorway]; (._;>;); out count; 145 ms 127 ms 0.88
[out:csv(::count)]; node[amenity=cafe](44.60,-63.66,44.70,-63.52); node(around:300)[shop]; out count; 103 ms 107 ms 1.04
[out:csv(::count)]; rel[boundary=administrative][admin_level=4][name="Nova Scotia"]; (._;>>;); out count; 720 ms 588 ms 0.82
[out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta; 354 ms 333 ms 0.94
[out:json]; way[building](44.640,-63.585,44.655,-63.565); out geom; 59 ms 56 ms 0.95
[out:json]; way[highway](44.60,-63.66,44.70,-63.52); out geom; 467 ms 452 ms 0.97
[out:csv(::count)]; node[highway=traffic_signals](44.60,-63.66,44.70,-63.52); way(bn); rel(bw); out count; 60 ms 57 ms 0.95
[out:xml]; nwr(44.640,-63.590,44.655,-63.570); (._;>;); out meta; 825 ms 755 ms 0.92
[out:csv(::count)]; way[highway]; node(w); out count; 1,617 ms 1,440 ms 0.89
[out:csv(::count)]; way[highway](44.645,-63.585,44.650,-63.575); node(w); out count; 30 ms 27 ms 0.92
[out:csv(::count)]; node(44.60,-63.66,44.70,-63.52); way(bn); out count; 100 ms 106 ms 1.06
[out:csv(::count)]; node[amenity]; way(bn); out count; 641 ms 513 ms 0.80
[out:csv(::count)]; rel[route=hiking]; (._;>;); way(bn); out count; 225 ms 199 ms 0.89
[out:csv(::count)]; way(id:24000000,25000000,26000000,30000000,40000000); out count; 3.8 ms 3.4 ms 0.89
[out:csv(::count)]; way[building]->.b; node(w.b)->.n; way(bn.n)[highway]; out count; 1,880 ms 1,806 ms 0.96

Memory

The filter takes about 2 bytes per id, next to the id list's own 8 bytes per id, and at most 16 MiB per predicate. Like the id list it sits in front of, it isn't counted toward [maxsize:]. Peak RSS is within 0.4 MB of master on 18 of the 19 queries. The exception is the largest id list, way[highway]; node(w), with 2.8M node ids: 144 → 152 MB, which is that query's 8 MiB filter.

Testing

  • Output: byte-identical to master on all 19 queries above.
  • Differential fuzzing (random queries compared against master's output): 10,000 queries across a plain, an area and a full-history database, 0 differences. This was run on an earlier base, with the same diff.
  • Checking the fuzzer catches this code: a copy of the change with a deliberate bug (dropping ids divisible by 97, about 1%) differed on 262 of 3,000 fuzz queries.

Limitations

  • Past about 8M ids the filter is at its 16 MiB cap, so more absent ids pass it (about 30% at 50M ids). Results stay correct, but the gain shrinks.
  • Compression: the gain is larger when nodes.bin and ways.bin are uncompressed, because the id test is then a bigger share of the work. On an earlier base, the first query above went from −22% to −48%.

https://claude.ai/code/session_01736kJWpJaGbt8ML6jz9N35


Generated by Claude Code

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

Id_Predicate::match tests each candidate object with std::binary_search
over the 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 reads every block of each quadtile that holds one of them and tests
every object in those blocks, so most of the binary searches fail.

Add Id_Bit_Filter, a hashed bit set of about 16 bits per id (capped at
16 MiB), and check it in Id_Predicate before the binary search. One bit
test rejects most absent ids; the binary search still decides every id
the filter lets through. Collect_Items now builds its Id_Predicate once in
its constructor instead of once per call to get_chunk, so the filter isn't
rebuilt for every chunk.

Median CPU time (user + system) of osm3s_query over 7 alternating runs,
warm page cache, 4-core 2.1 GHz Xeon VM, on Nova Scotia (data as of
2026-11-15, imported with --meta, LZ4 compression):

  [out:csv(::count)]; nwr[amenity=restaurant]; out count;
      159 ms -> 128 ms
  [out:csv(::count)]; rel[boundary=administrative][admin_level=4]
      [name="Nova Scotia"]; (._;>>;); out count;
      720 ms -> 588 ms

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
@cldellow
cldellow force-pushed the faster-id-predicate branch from 70f18bd to 30b37bc Compare November 28, 2026 03:38
@cldellow cldellow changed the title Reject non-matching ids with a bit filter before binary search Reject most non-matching ids before the binary search in Id_Predicate Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

good-candidate A good candidate for proposing upstream next - ranks well on ROI vs risk/review effort. 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.

2 participants