Skip to content

way geometry: reuse the way geometry this query has already built - #282

Open
cldellow wants to merge 1 commit into
masterfrom
geometry-way-cache
Open

cldellow wants to merge 1 commit into
masterfrom
geometry-way-cache

Conversation

@cldellow

@cldellow cldellow commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Fixes #280.

An alternative to #281, which caches node positions instead; only one of the two should be merged. See the comparison at the end.

Change

Resource_Manager (dispatch/resource_manager.h) gets a per-query cache of way geometry (way_geometry_cache, way id → coordinates) and geometry_cache_limit(). At the current date:

  • Way_Geometry_Store (data/way_geometry_store.cc) builds only the ways it doesn't find in the cache, adds complete geometries while under the cap (copying the input only when some of its ways are already cached), and get_geometry answers from the cache first;
  • Relation_Geometry_Store (data/relation_geometry_store.cc) passes the ids of cached member ways to relation_way_members to skip them, and takes their geometry from the cache, but only when it has no bounding box. With one (out bb (…), out center (…) and so on), members outside its ranges must stay without geometry, so it reads members as before.

Queries at a past date keep the old path. 5 files, +92/−8. No change to the database format or the query API.

Impact

CPU time (user + system), Nova Scotia extract of 2026-11-08 imported with --meta and lz4-compressed maps, with the area rules run, warm cache, 4-core Intel Xeon VM (2.1 GHz), against upstream/master 555f744, 10 alternating pairs; paired ratio with 95% CI. Output identical. The level-8 query is

[out:json][timeout:30]; area(3600390558)->.country; (way["boundary"="administrative"]["admin_level"="8"](area.country); relation["boundary"="administrative"]["admin_level"="8"](area.country); ); out geom;

The level-6 query is the same with "6" (benchmark p07); "ways only" and "relations only" keep only that statement of the union.

Query upstream/master This PR Ratio (95% CI) Peak RSS
level 8 2,764 ms 2,044 ms 0.730 (0.718–0.742) 50.5 → 49.0 MB
level 6 (p07) 553 ms 393 ms 0.697 (0.671–0.725) 14.5 → 13.8 MB
level 8, ways only 1,028 ms 749 ms 0.725 (0.700–0.752) 36.5 → 35.2 MB
level 8, relations only 1,638 ms 1,355 ms 0.808 (0.790–0.827) 37.9 → 37.6 MB

Peak memory falls slightly: later stores no longer hold node vectors for ways they find in the cache. The template on the public servers (admin level 4 in a country area, out geom) runs about 2,800 times a day at about 7.3 s each (#280).

Benchmark queries: timing workflow (run, commit 17cfe2f, GitHub runner, Nova Scotia): sum of medians 0.945 of upstream/master. The largest gains are on area and geometry queries; the cost is on single-statement queries that build way geometry but never reuse it, the worst being:

query ratio
[out:csv(::count)]; way[highway](44.60,-63.66,44.70,-63.52); out count; 1.070

That query's bbox filter builds every crossing way's geometry once, and filling the cache adds a hash insert and a copy per way. In callgrind (Nova Scotia, warm cache) it runs 3.4% more instructions than upstream/master; the first version, which also copied the input ways when none were cached, ran 6.1% more (CI ratio 1.090).

Memory

The cache lives for the whole query, so it is capped at a quarter of [maxsize:] (128 MiB at the default 512 MiB; 32 MiB without a limit), like the union block cache on the all-changes branch. Once full, nothing more is added and stores read from the database as before. It is auxiliary and isn't counted toward [maxsize:]. Each entry is estimated at 64 bytes plus 8 bytes per node of the way; a node shared by several ways is stored once per way.

Testing

  • Output identical to upstream/master 555f744 on the queries above, and on the level-8 query with [maxsize:8000000], where the cache is capped at 2 MB and fills up early.
  • The first CI run found 6 fuzz and 2 history differences, from relation output with a bounding box; see the comment below. Fixed in the amended commit.
  • CI on 17cfe2f: fuzz 5995 same, 5 skipped; history 2000 of 2000 same; props 60 seeds, no new failures.

Compared with #281

#281 (node positions) This PR (way geometry)
level 8 / level 6 0.768 / 0.734 0.730 / 0.697
relations only 0.832 0.808
peak RSS, level 8 +2.4 MB −1.5 MB
what's reused node lookups; relations still re-read their member ways node lookups and relations' member ways
partial geometry nodes reused individually only complete ways cached
size 2 files, +62/−4 5 files, +92/−8

This one is faster and lighter on these queries, and is the version on the all-changes branch; #281 is smaller.

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 29, 2026 — with Claude

Copy link
Copy Markdown
Owner Author

First CI run: 6 fuzz and 2 history differences, now fixed (amended commit 1fd6e9f).

  • fuzz (run): 6 differences, all queries that print relations with a bounding box on the output, for example .s1 out skel bb (45.011127,-63.097405,45.080386,-63.083169); or .s0 out meta center qt (46.140956,-60.160278,46.188058,-60.073987);.
  • history (run): 2 differences. Their artifacts are encrypted, so not inspected; presumably the same cause.

Cause: with a bounding box, Relation_Geometry_Store collects member ways only within the box's ranges, and members outside get no geometry. The first version skipped collecting cached member ways and took them from the cache, so members outside the box that earlier statements had cached gained geometry (an extra bounds in the output).

Fix: the relation store uses the cache only when it has no bounding box. Locally (Nova Scotia), all six fuzz queries differ from upstream/master on the first version and match it with the fix. The admin-boundary timings are unaffected (no output bounding box). CI reruns on the amended commit.


Generated by Claude Code

Each Way_Geometry_Store and Relation_Geometry_Store read the nodes (and,
for relations, the member ways) from the database, and every statement and
output that needs geometry builds its own store, so the same ways were read
and built again and again: for an area query with out geom, by the area
filters and again by out geom.

Resource_Manager gets a per-query cache of way geometry, capped at a quarter
of [maxsize:] (32 MiB without one) and outside its accounting like other
auxiliary structures. At the current date, Way_Geometry_Store builds only
the ways it doesn't find there, adding complete geometries while under the
cap, and get_geometry answers from it. Relation_Geometry_Store asks
relation_way_members to skip member ways whose geometry is cached and takes
it from the cache, when it has no bounding box: with one, members outside its
ranges must stay without geometry.

On the Nova Scotia extract of 2026-11-08 with the area rules run, warm
cache, 4-core VM, median CPU of 10 alternating runs:
  [out:json][timeout:30]; area(3600390558)->.country; (way["boundary"="administrative"]["admin_level"="8"](area.country); relation["boundary"="administrative"]["admin_level"="8"](area.country); ); out geom;
    2.76 s -> 2.04 s

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.

Way geometry is rebuilt by every statement and output that needs it

2 participants