Skip to content

bbox: don't load the geometry of ways whose index lies inside the bbox - #182

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

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

Conversation

@cldellow

@cldellow cldellow commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Fixes #181.

Change

Bbox_Filter::filter(const Statement&, Resource_Manager&, Set&) (data/bbox_filter.cc) now moves aside the current ways whose index area lies inside the bbox before it builds the Way_Geometry_Store, and puts them back after filtering the rest. A new contains_index_area takes the index's south-west and north-east node tiles from calc_bbox_bounds and compares their extreme coordinates with the bbox. It returns false for bboxes that cross the antimeridian or span 180 degrees or more, where Bbox_Double::intersects wraps longitudes.

Why this keeps the results: every node of a way lies in the area of its index. If that area is inside the bbox, so are the way's nodes; a way with one node is then contained, and any segment with both ends inside intersects. Without wrapping, Bbox_Filter::matches returns true for both.

Attic ways and relations are unchanged. One file, 29 lines added, 1 removed. No change to the database format or the query API.

Impact

US northeast full-history extract (Geofabrik), current-date queries, warm cache, 4-core VM, against upstream/master c3a20c0. Single runs (identical output); paired runs will follow in a comment.

Query upstream/master This PR
p16: 8 [key](bbox) statements, 1.65 × 3.58 degree box, out geom (below) 8.60 s 6.94 s
p01: 4 statements, 1 × 1 degree box, out center 1.51 s 1.25 s
p11: 4 statements, 7 × 13 degree box, out geom 3.63 s 3.03 s
p18: 2 statements, 6.1 × 10.3 degree box, out center 10.27 s 9.06 s
[out:json][maxsize:4000000000]; way(41.5,-74,43.5,-70.5); out count; peak RSS 5,080 MB 2,315 MB

Small boxes (p09, p19) show no change. The memory saving comes from not holding the geometry of the inside ways; queries with out geom still load it once for output.

p16: [out:json][timeout:120]; (way["maxheight"](41.2400,-73.5100,42.8900,-69.9300); way["maxweight"](41.2400,-73.5100,42.8900,-69.9300); way["maxlength"](41.2400,-73.5100,42.8900,-69.9300); way["maxwidth"](41.2400,-73.5100,42.8900,-69.9300); way["hazmat"](41.2400,-73.5100,42.8900,-69.9300); way["hgv"](41.2400,-73.5100,42.8900,-69.9300); node["maxheight"](41.2400,-73.5100,42.8900,-69.9300); node["maxweight"](41.2400,-73.5100,42.8900,-69.9300); ); out geom;
p01: [out:json][timeout:90]; (way["maxheight"]["highway"](42.0,-72.0,43.0,-71.0); way["bridge"]["maxheight"](42.0,-72.0,43.0,-71.0); way["tunnel"]["maxheight"](42.0,-72.0,43.0,-71.0); node["maxheight"]["highway"](42.0,-72.0,43.0,-71.0); ); out center;

Benchmark queries: the timing workflow runs them on this PR.

Testing

  • 50 targeted queries: way[highway], way[building], way[waterway], way[natural=water] and way (count) in ten boxes from 0.0001 degrees to 7 × 13 degrees, in Boston, New York, Maine, Vermont and on tile boundaries. 49 identical. The last, [out:json][maxsize:4000000000]; way(40.5,-80,47.5,-67); out count;, runs out of memory on upstream/master ("Query run out of memory using about 8653 MB of RAM", as the way geometry isn't counted towards [maxsize:]), and returns 19,560,764 ways with this PR, which doesn't build the geometry of the ways inside the box.
  • The 20 production-shaped queries: identical output.
  • 106 targeted queries on a Rhode Island full-history database: identical output.
  • CI: fuzz, history and props run on this PR.

Limitations

  • Inconsistent databases: a way that references a node missing from the database has no geometry and is dropped today. If its index area lies inside the bbox, this PR keeps it. The planet and complete-ways extracts have no such ways (OSM doesn't allow deleting a node a way still uses); clipped extracts can.
  • Bboxes across the antimeridian, or 180 degrees wide or more, are unchanged.
  • The same reasoning could apply to relations and to around and polygon filters; not done here.

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) 45353443173 5,986 same, 14 skipped, 0 differences
history (history,users,complete) 45353443283 1,999 same, 1 skipped, 0 differences
props 45353443180 passed
timing (benchmark queries, Nova Scotia, 5 runners) 45353443138 sum of medians 0.985; identical outputs

Beyond the noise floor: q04_bbox_tag −32.5%, q10_out_geom_highways −9.1%, t04_turbo_cycle_network −8.8%, q08_out_meta −3.4%, t03_turbo_wizard_bbox −3.0%, t06_josm_wizard_bbox −2.7%, t10_osmdata_bbox −2.0%, q09_out_geom_buildings −0.6%; t01_turbo_default +0.6% (floor ±0.5%).


Generated by Claude Code

Copy link
Copy Markdown
Owner Author

Paired runs on the 20 production-shaped queries (the public servers' heaviest templates of June 2026, moved to Boston and New England), US northeast full-history extract (Geofabrik), current-date queries, warm cache, 4-core VM, compare.py --paired --reps 10 against upstream/master c3a20c0. Identical output on all 20; sum of median CPU −6%.

Query Bbox Paired B/A (95% CI) B faster
p16 1.65 × 3.58 degrees, 6 way statements, out geom 0.810 (0.794–0.827) 10 of 10
p11 7 × 13 degrees, 3 way statements, out geom 0.885 (0.870–0.900) 10 of 10
p18 6.1 × 10.3 degrees, 1 way statement, out center 0.916 (0.901–0.931) 10 of 10
p01 1 × 1 degree, 3 way statements, out center 0.934 (0.898–0.971) 9 of 10
p19 1.25 × 1.9 degrees, 5 way statements 0.943 (0.926–0.960) 9 of 10

The others don't filter ways by a large bbox and are within noise (B/A 0.97–1.05, every CI including 1).


Generated by Claude Code

@cldellow

Copy link
Copy Markdown
Owner Author

Offered upstream in fossgis#22

To test ways against a bbox, Bbox_Filter loads the nodes of every
candidate way and checks its segments. It uses a fast path to exclude
ways that must lie outside of the bbox by virtue of their enclosing
tile. Extend the fast path to ways that must lie inside the bbox by
virtue of their enclosing tile.

On a Nova Scotia extract:

  [out:csv(::count)]; way[highway](44.60,-63.66,44.70,-63.52); out count;
      112 ms -> 74 ms (-34%)
  [out:json]; way[highway](44.60,-63.66,44.70,-63.52); out geom;
      396 ms -> 333 ms (-16%)

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bbox filters load the nodes of ways that lie inside the bbox anyway

2 participants