Repository navigation
Conversation
Looking up the node refs of a way or the members of a relation in a sorted vector visits ids in no particular order, so about half of std::binary_search's branches mispredict. These lookups now use a monobound search: it always runs log2(n) steps, and its only data-dependent step is a conditional add, compiled to a conditional move. Changed: binary_search_for_id and binary_search_for_pair_id (every caller looks up refs: way geometry, relation geometry, relation members in bbox and around filters and in member collection) and the has_a_child_with_id variants (way(bn), rel(bn), rel(bw), rel(br) and their role forms). Lookups that come in ascending id order, such as the elements of one tile or the tag index entries of one tag, keep std::binary_search, whose branches predict well there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm
Owner
Author
Timing
Unlike #227, no regressions on ARM beyond 1.3%, and the largest gains are in area and geometry queries (way geometry for polygons), which #227 and #229 don't touch. This change and #229 touch disjoint call sites, so they could be combined. Generated by Claude Code |
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.
Fixes #230.
Change
New header
core/monobound_search.h:monobound_index(index of the last element whose key isn't greater than the id) andmonobound_contains. The loop always runs log2(n) steps; its only data-dependent step is a conditional add, which GCC -O2 compiles to acmov(checked in the disassembly ofmake_geometryon x86).Used only where lookups come in no particular order:
binary_search_for_id,binary_search_for_pair_id(core/datatypes.h): every caller looks up node refs of ways or members of relations (way and relation geometry, relation members in the bbox and around filters and in member collection).has_a_child_with_id,has_a_child_with_id_and_role(data/abstract_processing.h):way(bn),rel(bn),rel(bw),rel(br)and their role forms.Lookups that mostly ascend (elements of one tile, tag index entries of one tag; the call sites of #227 and #229) keep
std::binary_search.4 files, +76/−33. No change to the database format or the query API.
One difference in behaviour, only if a vector held the same id twice: the old helpers returned whichever match the bisection hit first, the new ones the last match. The vectors searched hold one entry per id.
Impact
CI
timingruns on this PR (ARM); an x86 run (ubuntu-24.04) will follow. Results will be posted as a comment.Local, Geofabrik Nova Scotia extract with the area rules run, warm cache, 4-core x86 VM, 15 alternating pairs against
upstream/mastera9d8f15: no significant change (every 95% CI includes 1.0):[out:json]; node(44.60,-63.70,44.70,-63.50)->.n; way(bn.n); out count;[out:json]; way[highway](44.60,-63.70,44.70,-63.50); rel(bw); out count;[out:json]; rel[type=route](44.55,-63.75,44.75,-63.45); out geom;[out:json]; way[building](44.60,-63.70,44.70,-63.50); out center;[out:json]; rel(44.60,-63.70,44.70,-63.50); out count;[out:json]; way[building](44.640,-63.585,44.655,-63.565); out geom;[out:csv(::count)]; node(44.60,-63.66,44.70,-63.52); out count;(control, no ref lookups)Testing
bench/queries,bench/queries-areas,bench/queries-tools,bench/queries-poi, union test queries, and the queries above): output byte-identical toupstream/master. One further query,[out:json]; rel[type=route][route=bus](44.60,-63.70,44.70,-63.50)->.r; rel(around.r:50)[type=multipolygon]; out count;, timed out at 300 s on both builds and wasn't compared.fuzz,historyandpropsrun on this PR.Limitations
🤖 Generated with Claude Code
https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm