Skip to content

around: let the tag index drive queries whose around area is large - #13

Open
cldellow wants to merge 1 commit into
masterfrom
around-strategy
Open

cldellow wants to merge 1 commit into
masterfrom
around-strategy

Conversation

@cldellow

@cldellow cldellow commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Fixes #9.

Change

Around_Constraint::delivers_data (statements/around.cc) chose the query strategy from the radius alone: prefer_ranges for any radius under 2 km.

With prefer_ranges, the query statement reads every object in around's index ranges and applies tag filters afterwards. A small radius around a large input set, such as long relations, still covers a large area, so a selective tag filter went unused.

This keeps the radius rule and also looks at the area. When around's node index ranges cover more than 65,536 tiles (about one square degree), it returns ids_useful, so the tag index selects candidates within the ranges. Bbox_Constraint already applies the same rule (prefer ranges only under 1 square degree). The same filters apply either way, so results are unchanged.

One file, about 20 lines. Based on fossgis master. No change to the database format or the query API.

Impact

Wall time (single-threaded, so close to CPU time), Nova Scotia test database (Geofabrik extract of 2026-11-08, imported with --meta), warm cache, 4-core VM. Outputs are identical.

The prompting query (differential fuzzer seed 2, query 425, reduced):

[out:csv(::count)];
rel(45.566265,-62.428420,45.586924,-62.388716)->.s0;   // 9 relations touching a ~2 km box,
                                                       // including a long trail and boundaries
node(around.s0:0)["footway"];                          // nodes tagged footway lying on them
out count;
Tag filter on the same .s0 and radius 0 master This PR
["footway"] (the prompting query) 2.24 s 0.08 s
["building"] 2.49 s 0.59 s
["natural"] 4.95 s 2.85 s
["highway"] 13.3 s 11.4 s
["highway"="crossing"] 1.62 s 1.63 s
["source"] (very common) 52.7 s 51.9 s

The remaining cost for common tags is geometry testing, which #10 addresses. On master, the same footway query with radius 2000 already took 0.42 s, because the radius alone flipped the strategy.

around queries over small areas keep prefer_ranges and are unchanged, for example benchmark q06 and way(around:250,44.679085,-63.511161)->.s0; way(around.s0:100);.

Benchmark queries: all 11 have identical output. Times are within noise (−1% to −8%, total 2,474 → 2,380 ms; master from the cached baseline, so not alternating). None of them has a large-extent around, so no real change is expected.

Benchmark queries (from bench/queries/ on the bench-tools branch)
Name Query
q01_global_tag [out:csv(::count)]; nwr[amenity=restaurant]; out count;
q02_global_rare_tag [out:csv(::count)]; node[natural=peak]; out count;
q03_bbox_all_nodes [out:csv(::count)]; node(44.60,-63.66,44.70,-63.52); out count;
q04_bbox_tag [out:csv(::count)]; way[highway](44.60,-63.66,44.70,-63.52); out count;
q05_recurse_down [out:csv(::count)]; way[highway=motorway]; (._;>;); out count;
q06_around [out:csv(::count)]; node[amenity=cafe](44.60,-63.66,44.70,-63.52); node(around:300)[shop]; out count;
q07_big_relation [out:csv(::count)]; rel[boundary=administrative][admin_level=4][name="Nova Scotia"]; (._;>>;); out count;
q08_out_meta [out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta;
q09_out_geom_buildings [out:json]; way[building](44.640,-63.585,44.655,-63.565); out geom;
q10_out_geom_highways [out:json]; way[highway](44.60,-63.66,44.70,-63.52); out geom;
q11_recurse_up [out:csv(::count)]; node[highway=traffic_signals](44.60,-63.66,44.70,-63.52); way(bn); rel(bw); out count;

Bounding boxes are in Halifax, Nova Scotia; q07 names the Nova Scotia province boundary.

Testing

  • Differential fuzzer, seeds 2 and 3: 6,000 queries against master (bench/fuzz.py, see Benchmark queries and differential fuzzer #2). 0 differences, 12 skipped (all time out on master). Only 18 of these queries take the new path, which a temporary probe build confirmed.
  • Targeted check of the new path: 120 queries of the form rel(<small box around a random sampled node>)->.s0; node|way(around.s0:R)["k"="v"]; out ids;, with R in {0, 50, 500, 1999} and rare or moderately common tags from the fuzzer vocabulary. 82 took the new path, with 0 differences. 2 time out on both builds; they are way(around…) against long power lines, a geometry cost.
  • Update, re-fuzzed with the corrected fuzzer: this change has since been re-fuzzed as part of the all-changes branch (All changes combined (tracking PR) #12), with the corrected fuzzer and a bigger vocabulary (areas, history, users and complete). That covered 10,000 queries across the plain, area and history databases (6,000 / 2,000 / 2,000), with 0 differences. The per-PR fuzz run above predates a fuzzer fix: about 8% of its queries were invalid on both builds, so it rests on roughly 5,500 queries that ran (see Benchmark queries and differential fuzzer #2).

Memory

No new allocations beyond computing around's ranges once more during planning. get_ranges already recomputes them on each call.

Limitations

  • The threshold is untuned. It mirrors Bbox_Constraint's 1 square degree; the best cut-off may differ.
  • Common tags see less gain. With a large extent and a common tag, the tag index returns many ids. The cases measured were no slower, but production data may differ.
  • History and areas untested: [date:]/[diff:] queries and areas aren't covered, because the test database has no history and no areas.
  • Narrow test setup: one regional extract, warm cache, one VM.

Generated by Claude Code

Around_Constraint chose prefer_ranges whenever the radius was under
2 km. The query statement then reads every object in around's index
ranges and checks tag filters afterwards. A small radius around a
large input set, such as long relations, still covers a large area,
so a selective tag filter went unused.

Like Bbox_Constraint, also look at the area: with more than 65536 node
index tiles (about one square degree), return ids_useful so that the
tag index selects candidates within the ranges. The filters are the
same, so results are unchanged.

Nova Scotia extract:
  rel(45.566265,-62.428420,45.586924,-62.388716)->.s0;
  node(around.s0:0)["footway"];
takes 2.24 s -> 0.08 s. The 9 relations in .s0 include a long trail and
boundaries, so their extent covers much of the province. With radius
2000 the same query already took 0.42 s on master, because radius alone
decided the strategy.

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 21, 2026 — with Claude
@cldellow cldellow added the human-reviewed Colin has reviewed this. label Nov 21, 2026
@cldellow

Copy link
Copy Markdown
Owner Author

Colin's thoughts: low risk, high confidence, low-medium benefit.

This is about picking a better query strategy, following prior art in Bbox_Constraint. The query that prompted it is a little nonsensical (not a lot of node footways!), but that pattern could appear for other queries trying to find nodes on a relation.

@cldellow cldellow added the needs-followup Colin reviewed and wants Claude to iterate further, but didn't have spare agents available. label Nov 22, 2026
@cldellow

Copy link
Copy Markdown
Owner Author

Adding needs-followup as I think it'd be interesting to try to improve the common tags case. Common tags are common, so could probably just be hardcoded in (vs doing something smart like regular dumps from osmtaginfo)

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

Labels

human-reviewed Colin has reviewed this. needs-followup Colin reviewed and wants Claude to iterate further, but didn't have spare agents available. query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

around with a selective tag filter reads every candidate in the input set's extent first

2 participants