Skip to content

around: test input points through the tile index, not every point's bbox - #262

Open
cldellow wants to merge 1 commit into
masterfrom
around-bbox-points
Open

cldellow wants to merge 1 commit into
masterfrom
around-bbox-points

Conversation

@cldellow

@cldellow cldellow commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Fixes #261.

Change

Since upstream's bounding-box pre-check for around (f7f6d93), filter_nodes_expensive and filter_relations_expensive (statements/around.cc) call matches_bboxes(lat, lon) before is_inside(lat, lon) for each candidate node. matches_bboxes(lat, lon) scans node_bboxes, one box per input point, and stops only at a hit. Most candidates miss, so each one scans every input box, and around against a large node set becomes quadratic.

is_inside(lat, lon) already finds the input points near a coordinate through radius_lat_lons, a map keyed by tile, and that test is exact. So:

  • matches_bboxes(double, double) is removed, and its two callers call is_inside(lat, lon) directly.

  • is_inside(lat, lon) tests the way boxes (way_bboxes) after the points and before the segment loop. Each segment carries its way's box (add_way pushes std::make_pair(way_bbox, segment)), so a coordinate outside every way box can't pass the loop's own box test, and returning early gives the same answer.

  • Radius 0 now compares stored positions. is_inside(lat, lon) counted an input point as coincident when both coordinates differed by less than 1e-7. Coordinates are stored as integers in units of 1e-7° (::lat() is ilat / 10000000 - 91.0), so neighbours one unit apart differ by about 1e-7, and floating-point rounding put some of them just below it. The tolerance is now 0.5e-7: still far above rounding error, so the same position reached by different routes still matches, but neighbours never do.

The pre-check for way candidates, matches_bboxes(const Prepared_BBox&), is unchanged. Two files, 7 lines added, 13 removed. No change to the database format or the query API.

Results: the old pre-check passed a candidate inside any node box or way box, then is_inside decided. Now the point test runs first; the way-box test can only reject candidates the segment loop would reject.

The tolerance change settles the one case where those differ. Since 8063ce8 ("Fixed a bug in around for radius zero", 2018), which replaced exact equality with < 1e-7, nodes one storage unit apart (about 1 cm) could match each other with around:0. The pre-check (f7f6d93) stopped that for node candidates outside every input way's box, as a side effect of its 1e-8 box tolerance, but not inside one. Without the tolerance change, this PR would have brought those matches back everywhere; the first CI run showed this (1 fuzz difference, 99 props failures, all around:0 between one-unit neighbours; see the comment below). With it, one-unit neighbours never match, which is what the 2018 change evidently intended: tolerate rounding, not neighbours.

The one remaining difference from upstream/master 555f744: a candidate inside an input way's box and one unit from an input point matched on 555f744 and doesn't here.

Example, Geofabrik Nova Scotia extract of 2026-11-08 (node 5534330612 is at 44.8697970, -63.0928802; node 3491552732 one unit north, at 44.8697971):

[out:csv(::id;false)]; node(5534330612)->.a; node(around.a:0); out;

upstream/master a9d8f15 (before the pre-check) returns both nodes; 555f744 and this PR return 5534330612 alone.

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; medians and the paired ratio with 95% CI. Output identical on every query.

Query Pairs upstream/master This PR Ratio (95% CI) Peak RSS
R6 (the prompting query) 3 17.34 s 2.20 s 0.127 (0.120–0.134) 524 → 524 MB
J 3 43.32 s 43.33 s 1.002 (0.982–1.022) 299 → 299 MB
C 3 42.90 s 42.08 s 0.972 (0.953–0.990) 180 → 180 MB
D 3 23.49 s 21.57 s 0.932 (0.894–0.971) 1,107 → 1,107 MB
A 10 382 ms 375 ms 0.970 (0.870–1.081) unchanged
B 10 2,293 ms 2,224 ms 0.995 (0.919–1.078) unchanged
E 10 2,120 ms 2,042 ms 0.965 (0.929–1.004) unchanged
q06 10 91 ms 91 ms 1.042 (0.911–1.191) unchanged
p01 10 195 ms 191 ms 0.994 (0.953–1.036) unchanged
p04 10 607 ms 577 ms 0.975 (0.939–1.011) unchanged
p05 10 253 ms 251 ms 1.005 (0.957–1.055) unchanged
p08 10 170 ms 171 ms 1.028 (0.958–1.102) unchanged

On upstream/master a9d8f15, before the pre-check, R6 took 1.63 s with a peak of 338 MB. The remaining peak-memory increase comes from node_bboxes and the boxes stored with each point, which this PR doesn't remove.

Query Text
R6 [out:csv(::count)]; node(44.4,-64.0,45.0,-63.0); node(around:5)[amenity]; out count;
J [out:csv(::count)]; rel(45.566265,-62.428420,45.586924,-62.388716)->.s0; node(around.s0:0); out count;
C [out:csv(::count)]; way(44.648535,-63.573386,44.662526,-63.528647)->.s0; way(around.s0:100)["created_by"!~"^JO"]; out count;
D [out:csv(::count)]; rel(id:5429184,7081003,8322255,9270031,10270951,13448043)->.s0; .s0 <<->.s2; way(around.s2:0)[!"height"]; out count;
A [out:csv(::count)]; way(around:250,44.679085,-63.511161)->.s0; way(around.s0:100); out count;
B [out:csv(::count)]; nw(around:1000,44.790135,-65.396762)->.s0; way(around.s0:100)["width"!="1"]; out count;
E [out:csv(::count)]; rel(45.566265,-62.428420,45.586924,-62.388716)->.s0; node(around.s0:0)["footway"]; out count;
q06 [out:csv(::count)]; node[amenity=cafe](44.60,-63.66,44.70,-63.52); node(around:300)[shop]; out count;
p01 [out:json][timeout:30]; (node["amenity"="school"](around:3000,44.6488,-63.5752); way["amenity"="school"](around:3000,44.6488,-63.5752); node["amenity"="kindergarten"](around:3000,44.6488,-63.5752); node["amenity"="hospital"](around:5000,44.6488,-63.5752); way["amenity"="hospital"](around:5000,44.6488,-63.5752); node["amenity"="pharmacy"](around:3000,44.6488,-63.5752); node["shop"~"supermarket|convenience|mall"](around:2000,44.6488,-63.5752); way["shop"~"supermarket|convenience|mall"](around:2000,44.6488,-63.5752); node["highway"="bus_stop"](around:1500,44.6488,-63.5752); way["leisure"="park"](around:2000,44.6488,-63.5752); way["highway"~"primary|secondary|tertiary"](around:2000,44.6488,-63.5752); way["highway"~"residential|unclassified|service"](around:500,44.6488,-63.5752); way["landuse"="industrial"](around:3000,44.6488,-63.5752); node["amenity"="fuel"](around:3000,44.6488,-63.5752); ); out center body;
p04 [out:json][timeout:45]; (node["tourism"](around:25000,44.6488,-63.5752); way["tourism"](around:25000,44.6488,-63.5752); node["amenity"~"restaurant|cafe|bar|pub|fast_food|ice_cream"](around:25000,44.6488,-63.5752); way["amenity"~"restaurant|cafe|bar|pub"](around:25000,44.6488,-63.5752); node["leisure"~"park|garden|stadium|sports_centre|marina|golf_course"](around:25000,44.6488,-63.5752); way["leisure"~"park|garden|stadium|nature_reserve"](around:25000,44.6488,-63.5752); node["historic"](around:25000,44.6488,-63.5752); way["historic"](around:25000,44.6488,-63.5752); node["natural"~"peak|beach|waterfall"](around:25000,44.6488,-63.5752); ); out body center 500;
p05 [out:json][timeout:15]; (node["amenity"~"^(restaurant|fast_food|cafe|pharmacy|school)$"](around:4828,44.6488,-63.5752); way["amenity"~"^(restaurant|fast_food|cafe|pharmacy|school)$"](around:4828,44.6488,-63.5752); node["shop"~"^(supermarket|convenience|bakery)$"](around:4828,44.6488,-63.5752); way["shop"~"^(supermarket|convenience|bakery)$"](around:4828,44.6488,-63.5752); node["leisure"~"^(park|playground|fitness_centre)$"](around:4828,44.6488,-63.5752); way["leisure"~"^(park|playground|fitness_centre)$"](around:4828,44.6488,-63.5752); relation["leisure"~"^(park|recreation_ground)$"](around:4828,44.6488,-63.5752); ); out center tags;
p08 [out:json][timeout:30]; (node["amenity"="clinic"]["name"~"dental|medical|health|family|physio",i](around:25000,44.6488,-63.5752); way["amenity"="clinic"]["name"~"dental|medical|health|family|physio",i](around:25000,44.6488,-63.5752); node["healthcare"~"dentist|physiotherapist|optometrist",i](around:25000,44.6488,-63.5752); way["healthcare"~"dentist|physiotherapist|optometrist",i](around:25000,44.6488,-63.5752); ); out body center qt;

Benchmark sets, 3 pairs per query (an output check more than a timing): 21 queries on the plain database (q01–q12, p01–p06, p08–p10), sum of medians 6,922 → 6,952 ms, 1.00; 16 on the database with areas (a1–a4, t01–t11, p07), 6,826 → 6,913 ms, 1.01. All outputs identical. With 3 pairs, single queries scatter by up to ±20% (q06 1.21 here, against 1.04 at 10 pairs above).

Versions tried first:

  • Returning true whenever the input had points (so is_inside decides) fixed R6, but J, whose input mixes relation node members with long ways, ran into the CPU limit: it lost the way-box test.
  • Testing the tile lookup in the pre-check fixed R6, but made J 29% slower: with around:0, many more candidates share a tile with an input vertex than lie on one.
  • The exact point test in the pre-check, then again in is_inside, left J 6% slower from the repeated lookup.

Testing

  • Output identical to upstream/master 555f744 on every query above (timed before the tolerance change, which doesn't affect speed), and on the fuzz query that differed in the first CI run.
  • CI: fuzz, history and props rerun on the amended commit.

Limitations

  • is_inside(lat, lon) is also called for each node of a candidate way. It now scans the way boxes there too before the segment loop, which costs at most one box test per input way, against one per input segment in the loop that follows.
  • Peak memory stays at upstream/master's level (see above).

https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT


Generated by Claude Code

@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

CI differences, explained: all are around:0 between nodes one coordinate unit apart.

  • fuzz (run): 6,000 queries; 5,994 same, 5 skipped (timeouts), 1 different (plain, query 651 with seed 1). Its last statement, node(around.s2:0)["ele"!~"2"], returns 1,414 nodes here against 1,413 on 555f744.
  • props (run): 99 failures, all "output differs from the baseline", and every one is an around…:0 query. The micro and small synthetic sets place nodes 1e-7° apart.

Minimal case, Geofabrik Nova Scotia extract of 2026-11-08. Node 5534330612 is at 44.8697970, -63.0928802, and node 3491552732 at 44.8697971, -63.0928802, one unit (about 1 cm) north:

[out:csv(::id;false)]; node(5534330612)->.a; node(around.a:0); out;
Build Result
upstream/master a9d8f15 (before the bounding-box pre-check) 3491552732, 5534330612
upstream/master 555f744 5534330612
this PR 3491552732, 5534330612

Cause: is_inside(lat, lon) treats a point as coincident when std::abs(cit->lat - lat) < 1e-7 && std::abs(cit->lon - lon) < 1e-7. Coordinates one unit apart differ by just under 1e-7 in floating point, so they count as coincident. On 555f744 the pre-check runs first, and with radius 0 each input point's box is the point itself, compared with a 1e-8 tolerance, so the neighbour is rejected before is_inside sees it. The pre-check was meant to be an optimisation, but it changed this answer. This PR removes the pre-check for node candidates, so the answer goes back to what it was before f7f6d93. The fuzz and props differences are all this case; every other output is identical.

So this PR isn't output-neutral against current upstream/master. Whether around:0 should match nodes one unit apart is a question about the intended semantics, to settle before this goes further.


Generated by Claude Code

@cldellow cldellow added the bug Fixes wrong results (in addition to the kind label) label Nov 28, 2026 — with Claude

Copy link
Copy Markdown
Owner Author

History of the radius-0 test, from git log -L on the coincidence line in Around_Statement::is_inside(double, double):

Commit Date Subject The radius-0 case
5a17007 2011-07-08 Implemented and tested Around-Statement as standalone. none: great_circle_dist(...) <= radius only
3ed0247 2013-02-10 Accelerated the around statement. added as exact equality: cit->first == lat && cit->second == lon
8063ce8 2018-03-26 Fixed a bug in around for radius zero changed to std::abs(cit->first - lat) < 1e-7 && std::abs(cit->second - lon) < 1e-7
9bcaa62 2017-08-11 (merged later) Added around for polyline and tests. same test, members renamed to lat/lon
f7f6d93 2026 Use bounding boxes as pre-step to speed up around statement test unchanged; a pre-check with a 1e-8 box tolerance now runs before it

What the history does and doesn't say:

  • 8063ce8 is the only change aimed at radius 0. It changes this one line, with no message body, test or linked issue, so it records that exact equality was a bug but not which one.
  • The likely bug is rounding. The same node position can be computed by different routes, from the node index or from a way's geometry, and come out unequal in floating point.
  • 1e-7 is one storage unit. Coordinates are stored as integers in 1e-7° (::lat() returns ilat / 10000000 - 91.0). With a strict <, the evident intent is "the same stored position, allowing for rounding", with neighbours one unit apart just outside. In floating point, though, some one-unit differences come out slightly below 1e-7, so some neighbours (about 1 cm apart) matched. That reads as an accident at the tolerance's edge, not a decision.
  • No test pins the behaviour down. No test in this repository covers around:0 between distinct nodes. Upstream's test suite lives in the original repository, which wasn't searched.
  • The pre-check changed this case only as a side effect. f7f6d93 is described purely as a speed-up, and it made the result depend on whether an input way's box covers the candidate.

This PR's 0.5e-7 tolerance follows the inferred intent of 8063ce8: it still absorbs rounding, which is orders of magnitude smaller, but no longer admits neighbours one unit apart, whatever the input's ways.


Generated by Claude Code

cldellow commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner Author

CI on the amended commit (c0a46d4), with the 0.5e-7 tolerance:

  • fuzz (run): 6,000 queries; 5,995 same, 5 skipped (timeouts). 0 differences. The difference from the first run is gone.
  • history (run): 2,000 queries, all same.
  • props (run): 164 failures, all "output differs from the baseline", none in an equality check. Every one is an around…:0 query, mostly "date around rel = around members" with relation inputs. Across the 475 failure directories, the candidate output only removes lines relative to 555f744 (828 removed, 0 added). Each of the 668 removed nodes has another node exactly one storage unit (1e-7°) away in the synthetic data.

So these are the remaining case described above: on 555f744, a candidate inside an input way's box (relation inputs have way members) skipped the 1e-8 box test, and the 1e-7 coincidence test then matched one-unit neighbours. With the 0.5e-7 tolerance they never match. The synthetic micro, small, wide and tile sets place nodes on a 1e-7 lattice, so they hit this constantly; the real Nova Scotia data in fuzz (6,000 queries, plain, areas and extended) and history (2,000) doesn't hit it at all.

Example (props seed 7, wide): [date:"2020-01-01T00:00:01Z"]; relation(4)->.r; node(around.r:0);. On 555f744 it returns node 56 (at 44.6429663, -63.5870273), which isn't a member. It matches because node 95, a node of member way 27 at that date, lies one unit west at 44.6429663, -63.5870274.

The props failures shared with the baseline (area and poly ways) are unchanged.


Generated by Claude Code

Copy link
Copy Markdown
Owner Author

props re-run with #263 merged into bench-tools (run, attempt 2, props.py 8c1b5b0): 60 seeds, no new failures, 0 output differences from the baseline that aren't accepted. The around:0 one-unit-neighbour cases explained above now count as fixed. With fuzz (0 differences) and history (all same) on the same commit, CI is clean.


Generated by Claude Code

@cldellow cldellow removed the bug Fixes wrong results (in addition to the kind label) label Nov 28, 2026

Copy link
Copy Markdown
Owner Author

A real-data case of the radius-0 change. Geofabrik Nova Scotia extract of 2026-11-08:

[out:csv(::count)]; rel(id:5429184,7081003,8322255,9270031,10270951,13448043)->.s0; .s0 <<->.s2; way(around.s2:0)[!"height"]; out count;

upstream/master 555f744 returns 947; this PR returns 943 (and so does all-changes with it). None of the 4 ways it no longer returns (149816663, 746404860, 98556135, 98556177) has a node at the same position as an input node, but each has a node exactly one storage unit (1e-7°) from one, in latitude, longitude or both. These are the one-unit-neighbour matches the tolerance change removes, here in real data. fuzz didn't produce such a query.


Generated by Claude Code

Since the bounding-box pre-check (f7f6d93), filter_nodes_expensive and
filter_relations_expensive called matches_bboxes(lat, lon) before
is_inside(lat, lon). It scanned node_bboxes, one box per input point, for
every candidate node, so around against a large node set became quadratic.
is_inside already finds the input points near a coordinate through
radius_lat_lons, a map keyed by tile, and that test is exact.

Drop matches_bboxes(lat, lon). is_inside(lat, lon) now tests the way boxes
after the points and before the segments; each segment carries its way's
box, so a coordinate outside every way box can't be near a segment.

Also make radius 0 compare stored positions: is_inside counted points as
coincident when both coordinates differed by less than 1e-7, one storage
unit, so floating-point rounding let some neighbours one unit apart match.
Half a unit still absorbs rounding. Without this, dropping the pre-check
would have let those neighbours match again, as before f7f6d93.

[out:csv(::count)]; node(44.4,-64.0,45.0,-63.0); node(around:5)[amenity]; out count;
takes 17.3 s before and 2.2 s after (1.6 s before the pre-check), CPU,
Geofabrik Nova Scotia extract of 2026-11-08, warm cache, 4-core VM.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
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.

around: upstream's bounding-box pre-check scans every input box per candidate (R6 10x slower)

2 participants