Skip to content

around: don't load the geometry of ways whose index lies within the radius - #184

Open
cldellow wants to merge 1 commit into
masterfrom
around-ways-inside
Open

cldellow wants to merge 1 commit into
masterfrom
around-ways-inside

Conversation

@cldellow

@cldellow cldellow commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Fixes #183.

Change

Around_Constraint::filter(const Statement&, Resource_Manager&, Set&) (statements/around.cc) now moves aside the current ways for which the new Around_Statement::contains_index_area returns true before it builds the Way_Geometry_Store, and puts them back after filtering the rest.

contains_index_area(idx) takes the index's south-west and north-east node tiles from calc_bbox_bounds and returns true if, for one of the points, all four corners of that area are within radius - 1 m (great_circle_dist). It returns false when:

  • the statement has segments (polyline around, or ways or relations in the input), more than 64 points, or a radius of 1 m or less;
  • the area spans 180 degrees of longitude or more;
  • the area isn't inside the bbox that add_coord registers the point for (same formula, and only when that bbox doesn't wrap or reach 89.9 degrees).

Why this keeps the results:

  • Corners suffice. Over a lat/lon box narrower than 180 degrees, the great-circle distance to a point is greatest at a corner. Along a parallel, cos d falls monotonically with the longitude difference; along a meridian, cos d = R cos(lat - lat0) has a single maximum, so the distance has a single minimum. The distance has no maximum inside the box short of the antipode.
  • The nodes would match. Every node of a way with that index is within the radius of the point. It also lies in a tile add_coord registered the point for, so Around_Statement::is_inside(lat, lon) finds it, and the existing node pre-check in is_inside(way_geometry) returns true.

Rebased onto upstream/master 555f744, which adds a bounding-box pre-check to around (Prepared_BBox, matches_bboxes) and stores each point as std::pair<Prepared_BBox, Prepared_Point>. The conflict resolution keeps both pre-checks; contains_index_area reads the point from the pair's second member.

Attic ways and relations are unchanged. Two files, 63 lines added. No change to the database format or the query API.

Impact

CPU time (user + system) and peak RSS, warm cache, 4-core Intel Xeon VM (2.1 GHz), against upstream/master 555f744, in alternating pairs (15 per query) with the per-pair ratio of this build to the baseline. Two databases: the Geofabrik Nova Scotia extract of 2026-11-08, and the Geofabrik France extract (both imported with --meta and lz4-compressed maps). France replaces the US northeast extract used before the rebase, which is no longer available here; Paris is the densest area in it.

Query Database upstream/master This PR Paired ratio (95% CI) Faster in Peak RSS
H1 (the prompting query's shape) Nova Scotia 301 ms 101 ms 0.351 (0.326–0.378) 15 of 15 49 → 22 MB
H2 Nova Scotia 270 ms 115 ms 0.437 (0.407–0.469) 15 of 15 53 → 29 MB
F1 France 4.40 s 2.16 s 0.501 (0.479–0.523) 15 of 15 447 → 383 MB
F2 France 2.73 s 1.57 s 0.573 (0.550–0.597) 15 of 15 354 → 270 MB
p10 France 11.37 s 9.37 s 0.831 (0.818–0.844) 15 of 15 unchanged (91 MB)
p02 France 3.00 s 2.75 s 0.935 (0.909–0.961) 13 of 15 unchanged (33 MB)
p17 France 1.98 s 1.86 s 0.941 (0.911–0.973) 13 of 15 unchanged (36 MB)
p06 France 1.56 s 1.53 s 0.970 (0.933–1.009); not distinguishable 8 of 15 unchanged (33 MB)

Medians shown. All outputs are identical to the baseline.

Query Text
H1 [out:json]; way[highway](around:25000,44.6488,-63.5752); out count;
H2 [out:json]; way[building](around:8000,44.6488,-63.5752); out count;
F1 [out:json]; way[highway](around:25000,48.8566,2.3522); out count;
F2 [out:json]; way[building](around:8000,48.8566,2.3522); out count;

p02, p06, p10 and p17 are production-shaped queries (7–27 statements, around:500 to 25000) centred on Paris:

p02: [out:json][timeout:30]; (node["amenity"="school"](around:3000,48.8566,2.3522); way["amenity"="school"](around:3000,48.8566,2.3522); node["amenity"="kindergarten"](around:3000,48.8566,2.3522); way["amenity"="kindergarten"](around:3000,48.8566,2.3522); node["amenity"="hospital"](around:5000,48.8566,2.3522); node["amenity"="clinic"](around:5000,48.8566,2.3522); way["amenity"="hospital"](around:5000,48.8566,2.3522); node["amenity"="pharmacy"](around:3000,48.8566,2.3522); node["shop"~"supermarket|convenience|mall"](around:2000,48.8566,2.3522); way["shop"~"supermarket|convenience|mall"](around:2000,48.8566,2.3522); node["highway"="bus_stop"](around:1500,48.8566,2.3522); node["public_transport"="stop_position"](around:1500,48.8566,2.3522); node["railway"="station"](around:5000,48.8566,2.3522); node["railway"="halt"](around:5000,48.8566,2.3522); way["natural"="water"](around:3000,48.8566,2.3522); relation["natural"="water"](around:3000,48.8566,2.3522); way["landuse"="forest"](around:3000,48.8566,2.3522); relation["landuse"="forest"](around:3000,48.8566,2.3522); way["leisure"="park"](around:2000,48.8566,2.3522); way["highway"~"primary|secondary|tertiary"](around:2000,48.8566,2.3522); way["highway"~"residential|unclassified|service"](around:500,48.8566,2.3522); way["railway"~"rail|narrow_gauge"](around:1000,48.8566,2.3522); way["landuse"="industrial"](around:3000,48.8566,2.3522); node["landuse"="landfill"](around:5000,48.8566,2.3522); way["landuse"="landfill"](around:5000,48.8566,2.3522); node["amenity"="post_office"](around:3000,48.8566,2.3522); node["amenity"="fuel"](around:3000,48.8566,2.3522); ); out center body;

p06: [out:json][timeout:40]; (node["amenity"~"restaurant|cafe|ramen|sushi|izakaya|fast_food|bar|pub|food_court"](around:500,48.8566,2.3522); way["amenity"~"restaurant|cafe|ramen|sushi|izakaya|fast_food|bar|pub|food_court"](around:500,48.8566,2.3522); node["amenity"~"school|university|college|kindergarten"](around:1000,48.8566,2.3522); way["amenity"~"school|university|college|kindergarten"](around:1000,48.8566,2.3522); node["amenity"~"hospital|clinic|doctors|pharmacy"](around:5000,48.8566,2.3522); way["amenity"~"hospital|clinic|doctors|pharmacy"](around:5000,48.8566,2.3522); node["railway"~"station|halt"](around:1000,48.8566,2.3522); way["railway"~"station|halt"](around:1000,48.8566,2.3522); node["highway"="bus_stop"](around:1000,48.8566,2.3522); node["amenity"="parking"](around:500,48.8566,2.3522); way["amenity"="parking"](around:500,48.8566,2.3522); node["shop"="convenience"](around:500,48.8566,2.3522); way["shop"="convenience"](around:500,48.8566,2.3522); node["tourism"~"hotel|motel|hostel|guest_house|ryokan"](around:2000,48.8566,2.3522); way["tourism"~"hotel|motel|hostel|guest_house|ryokan"](around:2000,48.8566,2.3522); ); out center;

p10: [out:json][timeout:45]; (node["tourism"](around:25000,48.8566,2.3522); way["tourism"](around:25000,48.8566,2.3522); node["amenity"~"restaurant|cafe|bar|pub|nightclub|fast_food|food_court|biergarten|ice_cream|bakery"](around:25000,48.8566,2.3522); way["amenity"~"restaurant|cafe|bar|pub"](around:25000,48.8566,2.3522); node["amenity"~"theatre|cinema|museum|arts_centre|library|community_centre|concert_hall|studio"](around:25000,48.8566,2.3522); way["amenity"~"theatre|cinema|museum|arts_centre"](around:25000,48.8566,2.3522); node["leisure"~"park|garden|stadium|sports_centre|swimming_pool|beach_resort|water_park|marina|golf_course"](around:25000,48.8566,2.3522); way["leisure"~"park|garden|stadium|nature_reserve"](around:25000,48.8566,2.3522); node["shop"~"mall|department_store|supermarket|marketplace|clothes|books|gift|art|antiques|wine|craft|boutique"](around:25000,48.8566,2.3522); way["shop"~"mall|department_store|marketplace"](around:25000,48.8566,2.3522); node["historic"](around:25000,48.8566,2.3522); way["historic"](around:25000,48.8566,2.3522); node["natural"~"peak|beach|spring|cave_entrance|volcano|waterfall"](around:25000,48.8566,2.3522); node["man_made"~"tower|lighthouse|windmill"](around:25000,48.8566,2.3522); ); out body center 500;

p17: [out:json][timeout:15]; (node["amenity"~"^(restaurant|fast_food|cafe|pharmacy|school|food_court)$"](around:4828,48.8566,2.3522); way["amenity"~"^(restaurant|fast_food|cafe|pharmacy|school|food_court)$"](around:4828,48.8566,2.3522); node["shop"~"^(supermarket|grocery|convenience|bakery)$"](around:4828,48.8566,2.3522); way["shop"~"^(supermarket|grocery|convenience|bakery)$"](around:4828,48.8566,2.3522); node["leisure"~"^(park|recreation_ground|fitness_centre|fitness_center|dog_park|playground)$"](around:4828,48.8566,2.3522); way["leisure"~"^(park|recreation_ground|fitness_centre|fitness_center|dog_park|playground)$"](around:4828,48.8566,2.3522); relation["leisure"~"^(park|recreation_ground)$"](around:4828,48.8566,2.3522); ); out center tags;

The gain grows with the radius: with small radii, few way index areas fit inside the circle. Peak memory falls where it does because the moved-aside ways never enter the Way_Geometry_Store.

Testing

  • Before the rebase, on the US northeast extract: 120 targeted queries: way[highway], way[building], way["natural"="water"] and way[railway] with around of 50 m, 600 m, 2 km, 8 km, 25 km and 80 km around Boston, New York, the White Mountains, Providence and Portland (Maine), out ids: all identical to upstream/master.
  • The 20 production-shaped queries: identical output (before the rebase); after it, the eight queries above are identical against 555f744.
  • CI: fuzz, history and props rerun on the rebased branch.

Limitations

  • Inconsistent databases: as in bbox: don't load the geometry of ways whose index lies inside the bbox #182, a way that references a node missing from the database has no geometry and is dropped today. If its index area lies within the radius, this PR keeps it. Consistent databases (the planet, complete-ways extracts) have no such ways.
  • Polyline around, around from ways or relations, and more than 64 points are unchanged; so are relations.

https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT


Generated by Claude Code

@cldellow cldellow added query-memory query-perf Query performance change; collected on all-changes labels Nov 24, 2026 — with Claude

Copy link
Copy Markdown
Owner Author

CI on this PR, against upstream/master c3a20c0:

Workflow Run Result
fuzz (plain, areas,complete, extended sets) 45356386500 5,986 same, 14 skipped, 0 differences
history (history,users,complete) 45356386573 1,999 same, 1 skipped, 0 differences
props 45356386485 passed
timing (benchmark queries, Nova Scotia, 5 runners) 45356386539 sum of medians 1.000; identical outputs

The only benchmark query with around (q06) uses a 300 m radius around a node set, so no change is expected there.

Paired runs on the around queries among the 20 production-shaped ones (US northeast full-history extract, current-date queries, warm cache, 4-core VM, compare.py --paired --reps 10). Identical output on all six:

Query Radius Paired B/A (95% CI) B faster
p10 25,000 m, 14 statements 0.896 (0.882–0.910) 10 of 10
p02 500–5,000 m, 27 statements 0.965 (0.928–1.002) 8 of 10
p20 25,000 m, 4 statements, few ways 0.976 (0.955–0.998) 7 of 10
p17 4,828 m, 7 statements 0.995 (0.967–1.025) 7 of 10
p06 500–5,000 m, 15 statements 1.017 (0.974–1.062) 4 of 10
p12 25,000 m, 4 statements, few ways 1.056 (1.026–1.086) 1 of 10

p12 is dominated by case-insensitive regexes (59% of its CPU in glibc's regex code) and has only a few hundred candidate ways; in a profile of this build, contains_index_area and great_circle_dist don't appear at all. I'll re-measure it on a quiet machine before reading anything into it.


Generated by Claude Code

Copy link
Copy Markdown
Owner Author

Re-measured p12 on a quiet machine, compare.py --paired --reps 20 against upstream/master c3a20c0, with p20 (same shape) alongside:

Query Paired B/A (95% CI) B faster
p12 1.029 (0.977–1.083) 7 of 20
p20 0.996 (0.956–1.039) 10 of 20

Both are within noise; the 1.056 above didn't hold up. Identical output.


Generated by Claude Code

…adius

To test ways against around, Around_Constraint loads the nodes of every
candidate way and measures their distance. A way's index covers all its
nodes, so when the whole area of the index lies within the radius of a
point, every node of the way does, and the way matches. With a large
radius that is most of the ways, yet the filter loaded all their nodes.

For around with points (lat/lon, or input nodes, up to 64), move those
ways aside before building the geometry store, and keep them. Over a
lat/lon box narrower than 180 degrees the distance to a point is greatest
at a corner, so the four corners are tested, with a margin of 1 m. The
area must also lie in the bbox that add_coord registers the point for,
since is_inside only looks there.

The one difference: a way that references a node missing from the
database has no geometry, and was dropped before. With its index within
the radius it is now kept. Consistent databases have no such ways.

On a US northeast extract this query takes about half the CPU and a
third less memory:

[out:json]; way[highway](around:25000,42.3601,-71.0589); out count;
@cldellow cldellow added the good-candidate A good candidate for proposing upstream next - ranks well on ROI vs risk/review effort. label Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

good-candidate A good candidate for proposing upstream next - ranks well on ROI vs risk/review effort. query-memory query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

around filters load the nodes of ways that lie within the radius anyway

2 participants