Skip to content

Look up ids in sorted vectors with a forward cursor instead of a binary search - #229

Open
cldellow wants to merge 1 commit into
masterfrom
sorted-id-cursor
Open

cldellow wants to merge 1 commit into
masterfrom
sorted-id-cursor

Conversation

@cldellow

Copy link
Copy Markdown
Owner

Fixes #228.

Change

New header data/sorted_ids.h with sorted_contains(ids, pos, id): the same answer as std::binary_search(ids.begin(), ids.end(), id) for a sorted ids, but it keeps pos, where the previous lookup ended, and gallops forward from there. A run of ascending lookups costs about one comparison each instead of log2(n). It restarts at the beginning whenever id isn't greater than the element before pos, so any pos and any order of lookups give the right answer; it only needs ids sorted, as std::binary_search does.

It replaces std::binary_search at the same call sites as upstream pull request 14 (#227), as an alternative to it, except one: filter_ids_by_tags also searches new_ids, which isn't sorted there (ids restart with each value of a key), so that lookup keeps std::binary_search to keep results unchanged. 11 call sites in 4 files, plus the header. The cursor is an index, not an iterator, because filter_ids_by_tags appends to vectors during its loop.

No change to the database format or the query API.

Impact

Pending: the timing workflow runs on this PR (ARM); an x86 run (ubuntu-24.04) will follow, alongside the x86 run of #227. Results will be posted as a comment.

Testing

  • 52 queries (bench/queries, bench/queries-areas, bench/queries-tools, bench/queries-poi, plus union and rule-5 test queries) on the Geofabrik Nova Scotia extract with the area rules run, and the history database for the queries with [date:]: output byte-identical to upstream/master a9d8f15.
  • CI: fuzz, history and props run on this PR.

Limitations

  • Lookups that don't ascend (the first of each run) cost a full search plus a short gallop check, slightly more than std::binary_search.

Generated by Claude Code

…ry search

The loops that test elements against a sorted id list (late tag checks, tag collection for
output, filtering by ids, diff output) mostly look up ascending ids: tag index entries of one
tag, and the elements of one tile, come in id order. sorted_contains keeps where the previous
lookup ended and gallops forward from there, so a run of ascending lookups costs about one
comparison each instead of log2(n). It restarts from the beginning when an id isn't greater
than the element before the cursor, so any order of lookups gives the same answer as
std::binary_search.

Same call sites as upstream pull request 14 (monobound binary search), as an alternative to it,
except the lookup in filter_ids_by_tags's new_ids: that vector isn't sorted there (ids restart
with each value of a key), so it keeps std::binary_search to keep results unchanged.

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.980, 19 of 28 queries faster, none slower. A/A controls 0.998–1.002.
  • x86 (ubuntu-24.04, AMD EPYC 9V45 / 9V74, Intel Xeon 6975P-C, run): total 0.980, 17 faster, 2 slower (q01_global_tag +1.6%, q02_global_rare_tag +3.0%). A/A controls 0.994–1.004.

Against #227 (monobound search at the same call sites): on ARM this PR is 2% faster where #227 is 2.3% slower; on x86 both total 0.98, with #227 ahead on the global tag queries and this PR ahead on out geom highways.

Query ARM, this PR ARM, #227 x86, this PR x86, #227
t05_josm_full_download 0.923 1.069 0.944 0.947
q12_josm_download 0.926 1.047 0.954 0.959
q08_out_meta 0.958 1.060 0.967 0.957
q09_out_geom_buildings 0.958 1.099 0.981 0.963
t11_mapcomplete 0.964 1.121 0.976 0.970
q10_out_geom_highways 0.975 1.023 0.969 0.986
q01_global_tag 0.991 0.999 1.016 0.967
q02_global_rare_tag 0.991 1.001 1.030 0.949
Total 0.980 1.023 0.980 0.979

Open question: why the global tag queries (q01, q02) are slower with the cursor on x86 but not on ARM. A likely place is lookups that don't ascend, where the cursor pays a full search plus the restart check.


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.

Id lookups in sorted vectors: monobound binary search is slower; a forward cursor may be faster

2 participants