Skip to content

filter_ids_by_tags: test key-regex matches against one candidate list - #260

Open
cldellow wants to merge 1 commit into
masterfrom
tags-regex-one-search
Open

cldellow wants to merge 1 commit into
masterfrom
tags-regex-one-search

Conversation

@cldellow

@cldellow cldellow commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

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_ids if it's non-empty, otherwise new_ids. The answer is the same for every entry:

  • Before the coarse index's first exact key filter, old_ids is empty and new_ids is the input candidate list, sorted by generate_ids_by_coarse. Only the new_ids search could succeed, and it's the one that now runs.
  • 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. If old_ids is empty at this point, the candidates ran out, and new_ids is empty too.

After the loop, the regex matches are intersected with the final, sorted new_ids as 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 --meta and lz4-compressed maps, warm cache, 4-core Intel Xeon VM (2.1 GHz), against upstream/master 555f744. 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):

Query Text Pairs Ratio (95% CI) Faster in
R5 [out:csv(::count)]; nwr[highway](around:300,44.6488,-63.5752)[~"."~"."]; out count; 50 0.916 (0.875–0.959) 40 of 50 (sign test p < 0.001)
R3 [out:csv(::count)]; nwr[amenity](around:2000,44.6488,-63.5752)[~"."~"."]; out count; 50 0.986 (0.959–1.014) 34 of 50 (p = 0.02)
R1 [out:csv(::count)]; nwr[highway][~"^name"~"."](44.640,-63.590,44.655,-63.570); out count; 50 1.020 (0.986–1.055) 21 of 50
R2 [out:csv(::count)]; way[building][~"^addr:"~"."](44.60,-63.66,44.70,-63.52); out count; 25 0.976 (0.936–1.018) 15 of 25
R4 [out:csv(::count)]; nwr[~"^(shop|amenity)$"~"."](44.60,-63.66,44.70,-63.52); out count; 25 0.987 (0.931–1.045) 11 of 25
R6 [out:csv(::count)]; way[building](44.640,-63.590,44.655,-63.570)[~"."~"."]; out count; 25 0.972 (0.907–1.043) 16 of 25

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.

Query Text
p02 [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;
p07 [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 small around), 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

  • Output identical to upstream/master 555f744 on the 6 key-regex queries and 37 benchmark queries above.
  • CI: fuzz (whose vocabulary includes key-regex filters), history and props run on this PR.

Limitations

  • Narrow test setup: one regional extract, warm cache, one VM.
  • The argument that the second search never matters rests on filter_ids_by_tags getting a sorted input list. Both callers (Query_Statement's late tag check, and filter_ids_by_tags_old) pass lists built by generate_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

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
@cldellow cldellow added the query-perf Query performance change; collected on all-changes label Nov 27, 2026 — with Claude

Copy link
Copy Markdown
Owner Author

Why R5 gains, and why realistic queries don't. Instruction counts from valgrind --tool=callgrind: one run each, Geofabrik Nova Scotia extract of 2026-11-08 imported with --meta and lz4-compressed maps. Unlike timings, these counts are deterministic.

R5, [out:csv(::count)]; nwr[highway](around:300,44.6488,-63.5752)[~"."~"."]; out count;:

upstream/master 555f744 This PR Change
Whole query 579.2 M 545.9 M −5.8%
filter_ids_by_tags (both instantiations) 480.9 M 447.6 M −33.3 M

The saving is entirely in filter_ids_by_tags. It comes from entries where old_ids is non-empty and the object isn't in it. In that case the old code searched new_ids as well, while the new code stops. (When old_ids is empty, both versions do the same single search of new_ids.) In R5:

  • once the walk passes the highway key, old_ids holds the candidates, the objects within 300 m;
  • [~"."~"."] matches every later tag (name, oneway, source, surface, ...) of every object in the coarse index;
  • most of those objects are outside the circle, so each such tag cost one wasted search.

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:

Query upstream/master This PR Change
[out:csv(::count)]; nwr[amenity](around:1000,44.6488,-63.5752)[~"^name(:.*)?$"~"tim",i]; out count; 288.20 M 288.19 M −0.001%
[out:csv(::count)]; way[highway](44.640,-63.590,44.655,-63.570)[~"^maxspeed(:.*)?$"~"."]; out count; 151.99 M 151.73 M −0.17%
[out:csv(::count)]; nwr[shop](around:1000,44.6488,-63.5752)[~"^(contact:)?(phone|website)$"~"."]; out count; 275.20 M 275.18 M −0.01%
[out:csv(::count)]; way[building](around:500,44.6488,-63.5752)[~"^addr:"~"."]; out count; 206.26 M 206.46 M +0.10%
[out:csv(::count)]; nwr[amenity](around:1000,44.6488,-63.5752)[~"^(disused|abandoned|was):"~"."]; out count; 262.53 M 262.53 M −0.001%

Outputs are identical. The addr: query can't gain anything: addr:* keys sort before building, so old_ids is still empty when they're checked. Its +0.10% is code layout.

R5 itself isn't a realistic query. [~"."~"."] means "has at least one tag", which is redundant next to [highway]. On its own (for example, tagged nodes in a box) it doesn't benefit either, because without an exact key filter old_ids stays empty.

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

Copy link
Copy Markdown
Owner Author

Is the search of the unsorted new_ids undefined behaviour? Not today, but it was formally undefined before 9641011 ("Use monobound binary search in strategic places"). Query results were never affected either way.

  • Today the line is monobound_binary_search(new_ids, id) (data/abstract_processing.h). Its loop keeps lower + upper <= vec.size(), so every index it reads is in bounds whatever the values. The element order only decides which path it takes. It's ordinary code with no stated precondition, so on an unsorted vector it just may return a wrong false.
  • Before 9641011 the line was std::binary_search(new_ids.begin(), new_ids.end(), id). The standard library requires the range to be partitioned with respect to the value, and breaking a library precondition is undefined behaviour. libstdc++'s implementation, a lower_bound loop that only reads within [begin, end), also just returned a possibly wrong bool. A build with libstdc++'s debug mode (-D_GLIBCXX_DEBUG), whose partition checks could abort on such a call, is the one place this could have shown up.
  • Either way, results are unaffected: any id the new_ids search could miss is found by the old_ids search first.

The change in #260 removes the call entirely: the single search left always runs on a sorted vector, old_ids or the sorted input in new_ids.


Generated by Claude Code

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

Labels

query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Key-regex filters do a redundant second id search per tag entry

2 participants