Repository navigation
Conversation
…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
Owner
Author
Timing
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
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 |
This was referenced Nov 26, 2026
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 #228.
Change
New header
data/sorted_ids.hwithsorted_contains(ids, pos, id): the same answer asstd::binary_search(ids.begin(), ids.end(), id)for a sortedids, but it keepspos, 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 wheneveridisn't greater than the element beforepos, so anyposand any order of lookups give the right answer; it only needsidssorted, asstd::binary_searchdoes.It replaces
std::binary_searchat the same call sites as upstream pull request 14 (#227), as an alternative to it, except one:filter_ids_by_tagsalso searchesnew_ids, which isn't sorted there (ids restart with each value of a key), so that lookup keepsstd::binary_searchto keep results unchanged. 11 call sites in 4 files, plus the header. The cursor is an index, not an iterator, becausefilter_ids_by_tagsappends to vectors during its loop.No change to the database format or the query API.
Impact
Pending: the
timingworkflow 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
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 toupstream/mastera9d8f15.fuzz,historyandpropsrun on this PR.Limitations
std::binary_search.Generated by Claude Code