Skip to content

Search for member and node refs with a branchless binary search - #231

Open
cldellow wants to merge 1 commit into
masterfrom
monobound-unpredictable
Open

cldellow wants to merge 1 commit into
masterfrom
monobound-unpredictable

Conversation

@cldellow

@cldellow cldellow commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Fixes #230.

Change

New header core/monobound_search.h: monobound_index (index of the last element whose key isn't greater than the id) and monobound_contains. The loop always runs log2(n) steps; its only data-dependent step is a conditional add, which GCC -O2 compiles to a cmov (checked in the disassembly of make_geometry on 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 timing runs 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/master a9d8f15: no significant change (every 95% CI includes 1.0):

Query B/A (95% CI)
[out:json]; node(44.60,-63.70,44.70,-63.50)->.n; way(bn.n); out count; 1.050 (0.981–1.118)
[out:json]; way[highway](44.60,-63.70,44.70,-63.50); rel(bw); out count; 0.975 (0.904–1.045)
[out:json]; rel[type=route](44.55,-63.75,44.75,-63.45); out geom; 0.976 (0.948–1.004)
[out:json]; way[building](44.60,-63.70,44.70,-63.50); out center; 0.960 (0.909–1.010)
[out:json]; rel(44.60,-63.70,44.70,-63.50); out count; 0.988 (0.909–1.067)
[out:json]; way[building](44.640,-63.585,44.655,-63.565); out geom; 0.964 (0.841–1.086)
[out:csv(::count)]; node(44.60,-63.66,44.70,-63.52); out count; (control, no ref lookups) 0.997 (0.943–1.052)

Testing

  • 51 queries (bench/queries, bench/queries-areas, bench/queries-tools, bench/queries-poi, union test queries, and the queries above): output byte-identical to upstream/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.
  • CI: fuzz, history and props run on this PR.

Limitations

  • The local VM is too noisy to resolve a few percent; the CI runners are the real measurement.

🤖 Generated with Claude Code

https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm

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

Copy link
Copy Markdown
Owner Author

Timing

timing workflow, 5 runners each, 5 alternating runs per query per build, against upstream/master a9d8f15. Outputs identical on every query on both.

  • ARM (ubuntu-24.04-arm, Neoverse-N2, run): total 0.991, 11 of 28 queries faster, 2 slower (q11_recurse_up +1.3%, q01_global_tag +0.3%). A/A controls 0.998–1.002.
  • x86 (ubuntu-24.04, run): total 0.973, 15 faster, 1 slower (q03_bbox_all_nodes +1.8%, a control query this change doesn't touch). One runner's A/A control was 1.033 and is flagged by the summary as suspect, so the x86 figures are less firm than the ARM ones.
Query ARM B/A x86 B/A
a3_buildings_hrm 0.984 0.925
q04_bbox_tag 0.977 0.933
q11_recurse_up 1.013 0.957
a2_highways_halifax 0.988 0.958
a4_is_in_cafes 0.988 0.961
q09_out_geom_buildings 0.984 0.970
q10_out_geom_highways 0.992 0.970
t08_osmnx_graph_drive 0.983 0.988
Total 0.991 0.973

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Binary searches over member and node refs visit ids in no particular order

2 participants