Repository navigation
Conversation
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. |
This was referenced Nov 21, 2026
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
force-pushed
the
faster-id-predicate
branch
from
November 28, 2026 03:38
70f18bd to
30b37bc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change
Id_Predicate::matchtests each candidate object withstd::binary_searchover the sorted id list. Most candidates are not in the list: a tag query such asnwr[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.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_Predicatechecks the filter first. The binary search still decides every id the filter lets through, so results can't change.Collect_Itemsbuilds itsId_Predicateonce, in its constructor, instead of once perget_chunk, so the filter isn't rebuilt for every chunk. The predicate holds the filter through ashared_ptrbecause predicates are copied by value (Not_Predicate,And_Predicate, function arguments).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'shash_64(GOLDEN_RATIO_64) and Boost'shash_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_queryover 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--metaand LZ4 compression. Baseline:master555f744.master[out:csv(::count)]; nwr[amenity=restaurant]; out count;[out:csv(::count)]; node[natural=peak]; out count;[out:csv(::count)]; way[highway=motorway]; (._;>;); out count;[out:csv(::count)]; rel[boundary=administrative][admin_level=4][name="Nova Scotia"]; (._;>>;); out count;[out:csv(::count)]; node[amenity]; way(bn); out count;[out:csv(::count)]; rel[route=hiking]; (._;>;); way(bn); out count;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
master[out:csv(::count)]; nwr[amenity=restaurant]; out count;[out:csv(::count)]; node[natural=peak]; out count;[out:csv(::count)]; node(44.60,-63.66,44.70,-63.52); out count;[out:csv(::count)]; way[highway](44.60,-63.66,44.70,-63.52); out count;[out:csv(::count)]; way[highway=motorway]; (._;>;); out count;[out:csv(::count)]; node[amenity=cafe](44.60,-63.66,44.70,-63.52); node(around:300)[shop]; out count;[out:csv(::count)]; rel[boundary=administrative][admin_level=4][name="Nova Scotia"]; (._;>>;); out count;[out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta;[out:json]; way[building](44.640,-63.585,44.655,-63.565); out geom;[out:json]; way[highway](44.60,-63.66,44.70,-63.52); out geom;[out:csv(::count)]; node[highway=traffic_signals](44.60,-63.66,44.70,-63.52); way(bn); rel(bw); out count;[out:xml]; nwr(44.640,-63.590,44.655,-63.570); (._;>;); out meta;[out:csv(::count)]; way[highway]; node(w); out count;[out:csv(::count)]; way[highway](44.645,-63.585,44.650,-63.575); node(w); out count;[out:csv(::count)]; node(44.60,-63.66,44.70,-63.52); way(bn); out count;[out:csv(::count)]; node[amenity]; way(bn); out count;[out:csv(::count)]; rel[route=hiking]; (._;>;); way(bn); out count;[out:csv(::count)]; way(id:24000000,25000000,26000000,30000000,40000000); out count;[out:csv(::count)]; way[building]->.b; node(w.b)->.n; way(bn.n)[highway]; out count;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 ofmasteron 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
masteron all 19 queries above.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.Limitations
nodes.binandways.binare 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