Repository navigation
Conversation
For a tag entry that matches a key-regex filter, filter_ids_by_tags searched old_ids and, if that failed, new_ids. Before the coarse index's first exact key filter, old_ids is empty and new_ids is the sorted input, so only the new_ids search can succeed. From the first exact key filter on, every id in new_ids was just found in old_ids, so the new_ids search, on a vector that is then unsorted, can't find anything old_ids doesn't. Search old_ids if it's non-empty, otherwise new_ids. When old_ids is empty after an exact key filter, new_ids is empty too, so the answer is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
|
Why R5 gains, and why realistic queries don't. Instruction counts from R5,
The saving is entirely in
The paired timing, 0.916 (0.875–0.959), is a little more than the instruction saving. Those searches were on an unsorted vector and probably missed in cache too, but the instruction count is the firmer number. Realistic key-regex queries gain nothing measurable. A regex that matches few keys, or few values, makes few entries reach the check. Same setup:
Outputs are identical. The R5 itself isn't a realistic query. So this PR is a simplification: it removes a search whose result never matters, on a vector that isn't sorted. Its speed-up only shows with catch-all key regexes alongside an exact key filter. Generated by Claude Code |
|
Is the search of the unsorted
The change in #260 removes the call entirely: the single search left always runs on a sorted vector, Generated by Claude Code |
Fixes #259.
Change
filter_ids_by_tags(data/filter_ids_by_tags.h) applies key-regex filters ([~"k"~"v"]) while it walks a coarse index's local tag entries. For an entry whose key and value match, it checked whether the object is still a candidate with two searches:old_ids, then, if that failed,new_ids.Now it searches one list:
old_idsif it's non-empty, otherwisenew_ids. The answer is the same for every entry:old_idsis empty andnew_idsis the input candidate list, sorted bygenerate_ids_by_coarse. Only thenew_idssearch could succeed, and it's the one that now runs.new_idswas just found inold_ids, so thenew_idssearch (on a vector that is then unsorted) can't find anythingold_idsdoesn't. Ifold_idsis empty at this point, the candidates ran out, andnew_idsis empty too.After the loop, the regex matches are intersected with the final, sorted
new_idsas before.What it saves: one binary search per matching tag entry whose object isn't a candidate, which is common when the candidates are a small part of the coarse index. One file, 1 line added, 2 removed. No change to the database format, the query API or the results.
Impact
CPU time (user + system), Geofabrik Nova Scotia extract of 2026-11-08 imported with
--metaand lz4-compressed maps, warm cache, 4-core Intel Xeon VM (2.1 GHz), againstupstream/master555f744. Alternating pairs, with the per-pair ratio of this build to the baseline. Output identical on every query; peak RSS within 4% (median of a few hundred kB either way).Key-regex queries (none of the benchmark queries uses a key regex):
[out:csv(::count)]; nwr[highway](around:300,44.6488,-63.5752)[~"."~"."]; out count;[out:csv(::count)]; nwr[amenity](around:2000,44.6488,-63.5752)[~"."~"."]; out count;[out:csv(::count)]; nwr[highway][~"^name"~"."](44.640,-63.590,44.655,-63.570); out count;[out:csv(::count)]; way[building][~"^addr:"~"."](44.60,-63.66,44.70,-63.52); out count;[out:csv(::count)]; nwr[~"^(shop|amenity)$"~"."](44.60,-63.66,44.70,-63.52); out count;[out:csv(::count)]; way[building](44.640,-63.590,44.655,-63.570)[~"."~"."]; out count;R3 came out at 0.938 (0.908–0.970) in a first run of 25 pairs, and R1 at 1.073 (1.019–1.129); neither held up at 50 pairs.
Benchmark sets, 10 pairs per query: 21 queries on the plain database (q01–q12, p01–p06, p08–p10), sum of medians 6,822 → 6,849 ms, 1.004; 16 on the database with areas (t01–t11, a1–a4, p07), 6,914 → 6,820 ms, 0.986. Three CIs excluded 1 at 10 pairs: p02 1.032, t04 1.058 and p07 0.964. Re-run with 25 pairs, p02 is 1.000 (0.971–1.029) and t04 0.998 (0.959–1.039). None of them uses a key regex, so they don't run the changed line; I read them as noise.
[out:json][timeout:25]; (node["amenity"~"fuel|cafe"]["brand"~"tim hortons|irving|esso|shell|petro-canada|ultramar|circle k|needs|starbucks",i](44.45,-63.85,44.85,-63.35); node["amenity"~"fuel|cafe"]["name"~"tim hortons|irving|esso|shell|petro-canada|ultramar|circle k|needs|starbucks",i](44.45,-63.85,44.85,-63.35); way["amenity"~"fuel|cafe"]["brand"~"tim hortons|irving|esso|shell|petro-canada|ultramar|circle k|needs|starbucks",i](44.45,-63.85,44.85,-63.35); node["shop"~"supermarket|convenience"]["brand"~"sobeys|superstore|atlantic superstore|walmart|lawtons|shoppers drug mart|foodland",i](44.45,-63.85,44.85,-63.35); way["shop"~"supermarket|convenience"]["brand"~"sobeys|superstore|atlantic superstore|walmart|lawtons|shoppers drug mart|foodland",i](44.45,-63.85,44.85,-63.35); ); out center tags;[out:json][timeout:30]; area["name"="Nova Scotia"]["admin_level"="4"]->.p; (way["boundary"="administrative"]["admin_level"="6"](area.p); relation["boundary"="administrative"]["admin_level"="6"](area.p); ); out geom;t04 is overpass turbo's cycle network wizard query; full text:
bench/queries-tools/t04_turbo_cycle_network.ql.So the change is worth a few percent only with a catch-all key regex next to an exact key filter (R5,
[~"."~"."]on a smallaround), which isn't a realistic query:[~"."~"."]just means "has a tag". Five realistic key-regex queries (name:*in any language,maxspeed:*, contact keys,addr:*, lifecycle prefixes) show no change in instruction count (−0.17% to +0.10%; see the comment below). The main merit is that it removes a search whose result never matters, on a vector that isn't sorted.Memory
No new allocation.
Testing
upstream/master555f744 on the 6 key-regex queries and 37 benchmark queries above.fuzz(whose vocabulary includes key-regex filters),historyandpropsrun on this PR.Limitations
filter_ids_by_tagsgetting a sorted input list. Both callers (Query_Statement's late tag check, andfilter_ids_by_tags_old) pass lists built bygenerate_ids_by_coarse, which sorts them, or by a previous call, which ends by sorting.https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
Generated by Claude Code