Skip to content

around: take candidate ranges for relation inputs from their members - #38

Open
cldellow wants to merge 3 commits into
masterfrom
around-relation-ranges
Open

cldellow wants to merge 3 commits into
masterfrom
around-relation-ranges

Conversation

@cldellow

@cldellow cldellow commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Fixes #37.

Change

around's geometry for a relation in its input set is that of the relation's direct node and way members (calc_lat_lons). The candidate index ranges, though, came from the relation's own index. For a long route or a boundary, that's a compound index covering its whole bounding area, so every object there became a candidate and had its geometry loaded and tested.

Around_Statement::calc_ranges now uses, for current relations:

  • the ranges of their node members
  • the parent ranges of their way members

These are expanded by the radius as before. They cover all the geometry the exact test uses, so results are unchanged.

Two follow-up commits:

  • Relations at a past date ([date:], [diff:]): they now contribute the ranges of the members calc_lat_lons uses for them (node members, and way members as of the date) instead of their own index.
  • Members looked up once per input: calc_ranges runs several times per execution (6 times on the first query below: candidate ranges, the cheap filter twice, the exact filter). Each run fetched the relations' members again. Around_Statement now keeps the member ranges, together with the date, the current relations' ids, and the past-date relations' ids and timestamps. It computes them again only when one of those changes, so a foreach loop gets a fresh lookup for each new input.

Two files (around.cc, around.h), about 80 lines across the three commits. Based on fossgis master. No change to the database format or the query API.

Impact

CPU time (user + system) on a 4-core VM with a warm cache. Output is identical in every case.

This change alone, against fossgis master (8edf11b), on the plain database (Geofabrik Nova Scotia extract of 2026-11-08, imported with --meta):

Query master This PR
[out:csv(::count)]; rel(45.566265,-62.428420,45.586924,-62.388716)->.s0; node(around.s0:0)["footway"]; out count; (from #9; .s0 holds 9 relations, including a long-distance trail and boundaries) 2.32 s 0.51 s
[out:csv(::count)]; rel[route=hiking](44.60,-63.66,44.70,-63.52)->.s0; node(around.s0:50)[amenity]; out count; (small relations) 0.24 s 0.28 s (within noise)
Benchmark queries q01–q12 (#2) outputs identical; q06, the only one using around, unchanged

Without the grid from #10, around against large relation inputs still compares every candidate with every input segment (#3). So node(around.s0:0) with no tag filter, on the .s0 above, doesn't finish in minutes with or without this PR.

Together with the other around changes, on top of the all-changes branch (#12) with the grid revision of #36:

Query all-changes + grid revision + this PR
[out:csv(::count)]; wr(newer:"2016-12-19T18:03:25Z")(45.574002,-62.284014,45.619842,-62.234392)->.s0; way(around.s0:20)["material"!~"^conc"]; out count; (history database, current data; .s0 includes province-long road routes) 11.2 s 3.5 s
[out:csv(::count)]; rel(45.566265,-62.428420,45.586924,-62.388716)->.s0; node(around.s0:0); out count; 0.65 s, against 2.91 s on all-changes

The first query took over 120 s (timeout) on master, and 17.9 s on all-changes. Its cost is split between the two fixes: the grid revision takes it to 11.2 s, and this PR takes it to 3.5 s.

The two follow-up commits, against this PR's first commit (5 alternating runs per build; output identical). Plain database:

Query First commit All three
[out:csv(::count)]; rel(45.566265,-62.428420,45.586924,-62.388716)->.s0; node(around.s0:0)["footway"]; out count; 500 ms 440 ms (−12%)
[out:csv(::count)]; rel[route=hiking](44.60,-63.66,44.70,-63.52)->.s0; node(around.s0:50)[amenity]; out count; 210 ms 170 ms (−19%)
[out:csv(::count)]; rel[route=hiking](44.60,-63.66,44.70,-63.52); foreach->.r { node(around.r:50)[amenity]; out count; } 350 ms 310 ms (−11%)
[out:csv(::count)]; way(around:250,44.679085,-63.511161)->.s0; way(around.s0:100); out count; (no relations) 23.3 s 24.0 s (noise)

Full-history database (Geofabrik full-history extract of Nova Scotia, see #2):

Query First commit All three
[date:"2018-01-01T00:00:00Z"][out:csv(::count)]; rel(45.566265,-62.428420,45.586924,-62.388716)->.s0; node(around.s0:0)["footway"]; out count; 14.15 s, 1,271 MB 2.31 s, 245 MB
[diff:"2018-01-01T00:00:00Z","2026-11-19T00:00:00Z"]; rel(45.566265,-62.428420,45.586924,-62.388716)->.s0; node(around.s0:0)["footway"]; out ids; 27.6 s, 1,271 MB 4.7 s, 250 MB
[date:"2022-06-01T00:00:00Z"][out:csv(::count)]; rel[route=hiking](44.60,-63.66,44.70,-63.52)->.s0; node(around.s0:50)[amenity]; out count; 2.14 s, 173 MB 1.13 s, 67 MB
[out:csv(::count)]; rel(45.566265,-62.428420,45.586924,-62.388716)->.s0; node(around.s0:0)["footway"]; out count; (current data) 670 ms 570 ms (−15%)

Testing

The build tested was the all-changes branch plus the grid revision and this change (identical source to this commit on top of those).

  • Differential fuzzer (bench/fuzz.py) against all-changes:

    • plain database: 3,000 queries
    • areas, areas,complete: 1,500 queries
    • history database, users,complete: 1,500 queries

    0 differences in all three. The new build against itself: 300 queries, all identical.

  • Targeted check: 600 around queries whose input is one relation or way from the vocabulary, at radii from 0 to 1,000 m, against nodes, ways, relations and nwr, with and without tag filters. 445 had non-empty output. 0 differences.

  • Mutation test: a build that leaves the relations' way members out of the ranges was first confirmed to give wrong counts (3,690 instead of 12,809 on the first query above). It then differed on 265 of the 600 targeted queries, and on 3 of 1,000 fuzz queries.

  • Long-limit re-runs: the 27 queries from the degenerate-query hunt that time out or are slow on master (Benchmark queries and differential fuzzer #2) give identical output to all-changes, with a 900 s limit.

  • Follow-up commits:

    • Fuzzer against the first commit: 2,000 plain and 1,500 history queries (history,users,complete), 0 differences. The new build against itself: 300 history queries, all identical.
    • Targeted check: 400 around queries with relation inputs (by id, box, tag, or mixed with ways) on the history database, at radii 0–200 m. They cover current data, [date:] and [diff:] (dates 2010–2026), half of them inside a foreach whose input relation changes each iteration. Run on the all-changes branch (All changes combined (tracking PR) #12), against all-changes before these commits: 224 non-empty, 3 timeouts skipped, 0 differences. On this branch without the grid, these queries take minutes each. Before the run was moved, 30 queries had shown 2 apparent differences, both from the first commit hitting the query's own 120 s limit and printing partial output. With a 3,000 s limit, both outputs are identical (the first commit took 411 s and 172 s, this one 132 s and 84 s).
    • Mutation tests: a build that leaves the past-date relations' way members out of the ranges differed on that check from its first query. A build whose cache never recomputes differed on the hiking-route foreach loop above and on targeted queries 14, 20 and 21.

Memory

Bound: the cache keeps the member ranges and the input relations' ids (4 bytes each, 12 at a past date) until the statement is destroyed. That is bounded by the input relations and their members, which around loads for its geometry anyway.

The first commit adds no long-lived structure. The member lookup holds the relations' direct node and way members while the ranges are computed, which is bounded by the relations' member count. around loads the same members for its geometry anyway.

Peak memory (RSS) drops where relations are in the input, because far fewer candidates are loaded:

Query Before After
the #9 query above (master → this PR) 404.3 MB 89.7 MB
the province-long routes query above (all-changes → all-changes + grid revision + this PR) 1,132.7 MB 314.3 MB
[out:csv(::count)]; way(around:250,44.679085,-63.511161)->.s0; way(around.s0:100); out count; (no relations; all-changes → with both) 31.1 MB 31.3 MB

Limitations and open questions

  • calc_lat_lons still loads the members once more for the geometry. Sharing that with the cached ranges would save another lookup per execution, at the cost of a larger change.
  • Interaction with around: let the tag index drive queries whose around area is large #13 (all-changes only): around: let the tag index drive queries whose around area is large #13 lets the tag index choose candidates once the around ranges exceed 65,536 node tiles. The tighter member ranges can drop under that threshold, which is a loss for a query with a very selective tag at a past date. One such query ([date:"2018-01-01T00:00:00Z"][out:csv(::count)]; rel(45.566265,-62.428420,45.586924,-62.388716)->.s0; node(around.s0:0)["footway"]; out count;) went from 0.36 s to 1.97 s on all-changes. Over 118 generated past-date queries on all-changes, these commits cut total CPU by 46%, with no other query slower by more than 0.6 s. Details are in All changes combined (tracking PR) #12.
  • Relations of relations: a relation's relation members don't contribute geometry, here or upstream.
  • Long single ways still have coarse compound indexes; this change only affects relations.
  • Narrow test setup: one regional extract, warm cache, one VM.

https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT


Generated by Claude Code

around's geometry for a relation in its input set is that of the relation's
direct node and way members (calc_lat_lons). The candidate ranges, though, came
from the relation's own index, which for a long route or a boundary is a
compound index covering its whole extent: every object in that area became a
candidate and had its geometry loaded and tested.

calc_ranges now takes the ranges of the current relations' node members and
the parent ranges of their way members, expanded by the radius as before. These
cover all of the geometry the exact test uses, so results are unchanged. Attic
relations keep their own index.

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

Copy link
Copy Markdown
Owner Author

Colin thoughts: I think relations of relations aren't handled by this patch, but I believe they aren't handled by upstream, either.

Low risk, high confidence, high benefit.

Prevent relations from turning into degenerate queries by using a more granular set of tiles computed from their member ways/nodes rather than the bbox of the extents of the relation.

Possible followup work: extend it to attic, avoid the extra calc_ranges calls. Possibly worth doing as part of this PR, will check before opening an upstream PR.

@cldellow cldellow added human-reviewed Colin has reviewed this. needs-followup Colin reviewed and wants Claude to iterate further, but didn't have spare agents available. labels Nov 21, 2026
@cldellow cldellow added the query-memory label Nov 21, 2026 — with Claude
… members

The previous commit took around's candidate ranges for current relations in the
input set from their members' indexes. Relations at a past date ([date:] and
[diff:] queries) still contributed their own index, which for a long route or
a boundary covers its whole extent. They now contribute the ranges of the
members calc_lat_lons uses for their geometry: their node members and their
way members as of the date. Results are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
calc_ranges runs several times per execution of an around filter: for the
query's candidate ranges, twice in the cheap filter, and up to four times in
the exact filter. Since the previous commits, each run looked up the input
relations' node and way members to compute their index ranges. On the query
below, that was 6 lookups per execution and about a fifth of its run time.

The member ranges depend only on which relations are in the input and on the
date, so Around_Statement keeps them with those ids, the relations' timestamps
at a past date, and the date, and computes them again only when one of those
changes. In a foreach loop, a new input relation triggers a new lookup.

On the Nova Scotia extract:

  [out:csv(::count)]; rel(45.566265,-62.428420,45.586924,-62.388716)->.s0;
  node(around.s0:0)["footway"]; out count;

drops from 0.50 s to 0.44 s, with identical output.

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

Copy link
Copy Markdown
Owner Author

Both follow-ups are done, as two more commits on this PR:

  • Past dates: relations at a past date now take candidate ranges from their members too. The #9 query at a 2018 date goes from 14.2 s to 2.3 s, and peak memory from 1,271 to 245 MB. With [diff:] it goes from 27.6 s to 4.7 s.
  • Extra calc_ranges lookups: the member ranges are cached per input (keyed on the date and the relation ids), so the 6 lookups per execution become 1. Current-data relation inputs are 11–19% faster.

Testing: 3,500 fuzz queries and a 400-query targeted check, 0 differences; both mutants were caught. Details are in the description. Relations of relations are still not handled, as you noted, which matches upstream.


Generated by Claude Code

@cldellow cldellow removed the human-reviewed Colin has reviewed this. label Nov 22, 2026
@cldellow

Copy link
Copy Markdown
Owner Author

Removed human-reviewed label due to new commits from follow ups.

@cldellow cldellow removed the needs-followup Colin reviewed and wants Claude to iterate further, but didn't have spare agents available. label Nov 22, 2026
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

around with relations in the input reads every candidate in each relation's extent

2 participants