Skip to content

bbox filter: keep ways in inner tiles without loading their geometry - #21

Open
cldellow wants to merge 1 commit into
masterfrom
bbox-inner-tiles
Open

cldellow wants to merge 1 commit into
masterfrom
bbox-inner-tiles

Conversation

@cldellow

@cldellow cldellow commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Fixes #19.

Change

Bbox_Filter::filter (data/bbox_filter.cc) built a Way_Geometry_Store for every candidate way, reading the nodes of each one to test whether the way really meets the box.

This keeps, without loading geometry, every way whose index tile lies entirely inside the box:

  • Why that's safe: a way whose Uint31_Index is a single tile (bit 31 clear) has all its nodes in that tile. If the tile lies inside the box, so do all of the way's nodes. Bbox_Filter::matches accepts any way with a node inside the box: single nodes via contains(), segments via intersects().
  • Tile bounds are the positions decoded with the lowest and highest ll_lower. Every node position in a tile lies between them.
  • Assumes the way's nodes exist. A way's index is computed from its nodes when it is written, so the argument needs those nodes to still be in the database. OpenStreetMap's API refuses to delete a node that a way still uses, so planet-derived databases keep this property. On a database where it fails (a way whose nodes have all been deleted), the baseline excludes the way, because it can't build its geometry. This change keeps it, by its index tile. See Limitations.
  • Order is preserved: the vectors of those ways are moved aside before the exact test and moved back after, so output is identical.
  • Unchanged cases: boxes crossing the antimeridian, and attic (old) versions in [date:] and [diff:] queries, use the exact test as before. Objects unchanged since the queried date come from the current tables, so they take this path in date queries too.

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

Impact

CPU time (user + system), median of 5 alternating runs per build, Nova Scotia test database (Geofabrik extract of 2026-11-08, imported with --meta), warm cache, 4-core VM. Output is byte-identical for every query. The box (44.60,-63.66,44.70,-63.52) is central Halifax.

Query master This PR Change
[out:csv(::count)]; way(44.60,-63.66,44.70,-63.52); out count; 263 ms 133 ms −50%
[out:csv(::count)]; way[highway](44.60,-63.66,44.70,-63.52); out count; (benchmark q04) 132 ms 109 ms −18%
[out:json]; way[building](44.60,-63.66,44.70,-63.52); out tags; 455 ms 376 ms −17%
[out:json]; way[highway](44.60,-63.66,44.70,-63.52); out geom; (benchmark q10) 471 ms 438 ms −7% ¹
[out:csv(::count)]; nwr[amenity](44.60,-63.66,44.70,-63.52); out count; 200 ms 193 ms −4%
[out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta; (benchmark q08) 358 ms 346 ms −3%
[out:json]; way[building](44.640,-63.585,44.655,-63.565); out geom; (benchmark q09) 59 ms 58 ms −2% ²

¹ out geom loads the nodes for output anyway.
² In a small box, few tiles lie entirely inside.

Testing

This change skips work, so it got targeted checks beyond the standard fuzzer:

  • Tile bounds: 40 million decoded node positions in 200,000 random tiles all lie within the bounds computed from the lowest and highest ll_lower.
  • Random boxes: 600 queries of the form [out:csv(::id;false)]; way["k"](S,W,N,E); out ids; (one in three without a tag filter). The boxes were 0.001° to 0.5° high, at random positions around sampled nodes. 0 differences against master.
  • Mutation test: a build that wrongly accepts tiles extending up to 0.002° past the box's north and east edges differs on 50 of the same 600 queries, so the random-box check does catch boundary errors.
  • Differential fuzzer, seeds 2 and 3: 6,000 queries against master (bench/fuzz.py, see Benchmark queries and differential fuzzer #2). 0 differences; 15 skipped, all timeouts. That's three more than master's usual 12, because the run shared the CPU with a build.
  • 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 allocation beyond a map holding the moved vectors (moved, not copied). Peak memory (RSS) drops, because the skipped geometry is never loaded:

Query master This PR
all ways in the box, out count 58.2 MB 35.6 MB
q04 28.9 MB 25.5 MB
q10 31.2 MB 31.1 MB

Limitations

  • Differs on referentially broken data. On the history test database (see Benchmark queries and differential fuzzer #2), which has gaps from its regional filtering, this query returns 162 more objects than the baseline (15,078 against 14,916):

    [out:csv(::count)]; nwr(newer:"2026-11-18T00:00:00Z")(43.3,-66.5,47.1,-59.6); out count;
    

    All 162 are ways none of whose nodes exist in that database. On databases where every way's nodes exist, which OSM's API guarantees for planet-derived data, output is identical.

    Update: re-checked on a database imported directly from a full-history extract. That newer query now matches. A [date:"2018-01-01T00:00:00Z"] query returns 9 more ways than the baseline (1,788 against 1,779), and a [diff:] query differs too. All 9 are current versions whose nodes can't be resolved at that date. The direct import itself leaves ways whose index disagrees with their nodes' past positions, so this database isn't representative of production either. Whether production has such ways is unknown. Details in the comments.

  • Simple tile indexes only. Ways with compound indexes, which span several tiles, still take the exact test, even if their whole region is inside the box.

  • Relations and antimeridian boxes untested: relations are unchanged, and antimeridian-crossing boxes aren't covered by the test database. [date:] queries reach this change for objects unchanged since the date; see the update above.

  • Narrow test setup: one regional extract, warm cache, one VM.


Generated by Claude Code

Bbox_Filter::filter built a Way_Geometry_Store for every candidate way,
reading the nodes of each one to test whether it really meets the box.

A way whose index is a single tile (bit 31 clear) has all its nodes in
that tile. If the tile lies inside the box, so do all of its nodes, and
Bbox_Filter::matches accepts any way with a node inside. Keep such ways
without loading their geometry; test only the rest. A tile's bounds are
the positions decoded with the lowest and highest ll_lower. Boxes that
cross the antimeridian, and the history path, are unchanged.

Nova Scotia extract, CPU time, alternating runs:
  [out:csv(::count)]; way(44.60,-63.66,44.70,-63.52); out count;
    263 ms -> 133 ms
  [out:csv(::count)]; way[highway](44.60,-63.66,44.70,-63.52); out count;
    132 ms -> 109 ms

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

cldellow commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner Author

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

Accept ways that are trivially in the bbox; only do expensive geometry operations on ways we can't so prove.

The regression suite found a case where the new behaviour differs from the old behaviour: when querying a history with referentially broken objects: [out:csv(::count)]; nwr(newer:"2026-11-18T00:00:00Z")(43.3,-66.5,47.1,-59.6); out count;

I believe the history was broken due to how I constructed my test database. I'm retrying with a Geofabrik .osh.pbf, will see if that resolves it. Note that the .osh.pbf only has data up to Nov 15, so we'll need to test a variant of that query to confirm correctness. The query as is would trivially return 0 for both upstream and this patch.

Copy link
Copy Markdown
Owner Author

Re-check on a history database built from a full-history extract: the difference remains, and the new database is itself inconsistent at past dates.

I rebuilt the history test database by importing a full-history extract of Nova Scotia (an .osh.pbf, all versions from 2006 to 2026-11-15) directly with update_database --keep-attic. The extract is licensed and can't be shared, so only aggregate numbers are given here.

The database is right for current data and wrong for some ways at past dates

Checked against the extract with osmium:

  • Current data matches exactly: the same node, way and relation ids. 542 relations are found only by id, not by bbox, because none of their members has a known location in the extract.
  • Nodes at past dates match exactly. Node counts in the box (44.60,-63.66,44.70,-63.52) at 2010, 2014, 2018 and 2022 equal osmium's time-filter counts.
  • Ways at past dates don't. Overpass finds 48,679 ways in that box at 2018-01-01, osmium 54,565. Many old way versions can't resolve their nodes at the query date: out geom puts them at the "not found" position, and node(w) returns nothing.

The cause is the import, not the extract. When update_database computes a way's geometry, it takes node positions from the same update batch or from the current node table, never from attic (way_updater.cc, lookup_missing_nodes). A full-history file holds all node versions before the first way, so old way versions are built from today's nodes. Nodes deleted since aren't found, and nodes moved since give today's position. Production builds attic data from diffs in time order, where this doesn't happen. A history database built by replaying the extract in time order is being prepared separately.

Results on this database

Each query compared against fossgis master:

Query master This PR
[date:"2018-01-01T00:00:00Z"][out:csv(::count)]; nwr[amenity](44.60,-63.66,44.70,-63.52); out count; 1,779 1,788 (+9)
[diff:"2026-11-12T00:00:00Z","2026-11-19T00:00:00Z"]; nwr(44.60,-63.66,44.70,-63.52); out meta; output differs
[out:csv(::count)]; nwr(newer:"2026-11-08T00:00:00Z")(43.3,-66.5,47.1,-59.6); out count; same
[date:"2018-01-01T00:00:00Z"][out:csv(::count)]; way[highway=primary](44.60,-63.66,44.70,-63.52); (._;>;); out count; same
user filters (uid, uid_touched) on current data in that box same

master is deterministic on both differing queries (identical output on repeated runs). The newer query replaces the earlier newer:"2026-11-18…" check, which counts nothing on this extract.

All 9 extra objects in the [date:] query are ways whose current version predates 2018, and whose nodes can't be resolved at that date. master drops them because it can't build their geometry. This PR keeps them by their index tile. That is the same pattern as the earlier newer difference: ways whose index doesn't agree with their nodes' positions.

Correction to the description

The description says the history path is unchanged. That is only partly true. In a [date:] or [diff:] query, objects unchanged since the date come from the current tables, so they take this PR's path. Only attic versions keep the exact test. I'm updating the description.

What this means

  • Every difference seen so far involves a way whose index disagrees with the positions of its nodes at the time queried. On a database where index and geometry agree, output is identical.
  • Whether production has such ways is unknown. Diffs in time order should keep index and geometry consistent. But for example, data removed in the 2012 licence-change redaction may have left ways whose nodes can't be resolved at some dates. That hasn't been checked.
  • Open question: is "the way's index covers its nodes" an invariant Overpass guarantees, including for [date:] queries? If not, the inner-tile shortcut should be limited to queries without a date.

Generated by Claude Code

Copy link
Copy Markdown
Owner Author

Re-check on a correctly imported history database: no differences.

The history database was rebuilt from the same full-history extract with the importer fixes on the better-imports branch. Way ids at past dates now match osmium exactly, apart from 6 ways at 2010 whose source data references nodes missing at that date (#34). On it, this PR's output is identical to fossgis master for all six history benchmark queries (#2), including the two that differed before:

[date:"2018-01-01T00:00:00Z"][out:csv(::count)]; nwr[amenity](44.60,-63.66,44.70,-63.52); out count;
[diff:"2026-11-12T00:00:00Z","2026-11-19T00:00:00Z"]; nwr(44.60,-63.66,44.70,-63.52); out meta;

The all-changes branch was also identical on 1,999 of 2,000 history-feature fuzz queries (1 timeout).

So every difference seen so far came from broken test databases. The open question remains: can a production database contain a way whose index disagrees with its nodes' positions at a past date, for example after the 2012 redaction? The 6 ways above are that shape of data. Here they lack geometry on both builds, so they don't show up as a difference.


Generated by Claude Code

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. query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bbox filter reads the geometry of ways that lie entirely inside the box

2 participants