Skip to content

Meta_Collector: sorted vector instead of std::set - #31

Open
cldellow wants to merge 1 commit into
masterfrom
meta-collector-vector
Open

cldellow wants to merge 1 commit into
masterfrom
meta-collector-vector

Conversation

@cldellow

Copy link
Copy Markdown
Owner

Fixes #30.

Change

Meta_Collector loads one index tile's metadata at a time and then only searches it. It kept those entries in a std::set, so every load paid a node allocation and a red-black tree insert per entry, and every lookup walked the tree.

  • Flat storage: entries are appended to a std::vector, sorted only if they arrive out of order (blocks are normally already sorted), and searched with std::lower_bound.
  • Same lookup results:
    • duplicates are removed keeping the first, as std::set::insert did
    • reset() still adds to the existing entries without clearing them, as before
  • Pointer lifetime: get() returns a pointer into the container. Every caller uses it before calling get() again on the same collector, so a vector reallocation can't invalidate a pointer still in use.

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

Impact

CPU time (user + system), median of alternating runs against fossgis master (8edf11b), warm cache, 4-core VM. Output is identical for every query.

History database: Nova Scotia, imported directly from a full-history extract with --keep-attic. 5 runs per build. {{UID}} is the user id with the most current objects in the box. The extract is licensed, so the id isn't given.

Query master This PR Change
[out:csv(::count)]; nwr(newer:"2026-11-08T00:00:00Z")(43.3,-66.5,47.1,-59.6); out count; 5,224 ms 4,490 ms −14%
nwr(uid:{{UID}})(44.60,-63.66,44.70,-63.52); out meta; 1,661 ms 1,472 ms −11%
[out:csv(::count)]; nwr(uid_touched:{{UID}})(44.60,-63.66,44.70,-63.52); out count; 1,849 ms 1,945 ms +5% (within spread)
[diff:"2026-11-12T00:00:00Z","2026-11-19T00:00:00Z"]; nwr(44.60,-63.66,44.70,-63.52); out meta; 6,764 ms 6,685 ms −1%
[date:"2018-01-01T00:00:00Z"][out:csv(::count)]; nwr[amenity](44.60,-63.66,44.70,-63.52); out count; 2,026 ms 2,016 ms −1%
[date:"2018-01-01T00:00:00Z"][out:csv(::count)]; way[highway=primary](44.60,-63.66,44.70,-63.52); (._;>;); out count; 860 ms 881 ms +2%

For the uid_touched query, the two builds' ranges overlap (1,830–2,031 ms against 1,831–1,974 ms).

Plain database: Geofabrik Nova Scotia extract of 2026-11-08, imported with --meta. 7 runs per build.

Query master This PR Change
[out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta; (q08) 344 ms 316 ms −8%
[out:xml]; nwr(44.640,-63.590,44.655,-63.570); (._;>;); out meta; (q12, shaped like a JOSM download) 707 ms 660 ms −7%

The other benchmark queries (q01–q07, q09–q11; see #2) read no metadata. Cached-baseline runs put them within noise of master; the sum over q01–q12 is −4%.

Testing

  • Differential fuzzer (bench/fuzz.py on the bench-tools branch), against master:
    • This PR against itself: 300 queries, all identical (2 timeouts).
    • History database, with user filters: 2,000 queries with --features users, 0 differences (5 timeouts skipped). In a run with the same settings, 885 of 2,000 queries used metadata:
      • out meta: 499
      • user filters: 333
      • version(), timestamp(), changeset(), uid(), user() evaluators: 42–56 each
    • Plain database: 1,500 queries, 0 differences (2 timeouts skipped).

Memory

The vector replaces a set of the same entries. It's bounded by one tile's metadata, as before, and is smaller per entry (no tree node). Peak RSS doesn't change measurably:

Query master This PR
the newer query above 540.7 MB 543.5 MB
the uid query above 92.7 MB 92.9 MB
q12 25.6 MB 26.0 MB

Limitations and open questions

  • Modest gains: the set was about a fifth of the profile in the best case, and decompression and the node scan remain. Queries that read little metadata don't change.
  • The history test database is imperfect: a direct full-history import leaves some old way versions with unresolvable nodes (see bbox filter: keep ways in inner tiles without loading their geometry #21). That affects past-date way results, not the metadata path changed here, and both builds read the same data.
  • Narrow test setup: one regional extract, warm cache, one VM.

Generated by Claude Code

…:set

Meta_Collector loads the metadata of one index (tile) at a time and then only
searches it. It kept the entries in a std::set, so every load paid one node
allocation and a red-black tree insert per entry, and every lookup walked the
tree. Queries that read metadata for many objects (out meta, newer, user
filters) spent about a fifth of their time there.

Now the entries are appended to a vector, sorted if needed (blocks are usually
already in order), and searched with std::lower_bound. Duplicates are removed
keeping the first, as std::set::insert did, and reset() still adds to the
existing entries without clearing them, so lookups return the same entries as
before. Callers use the returned pointer immediately, before the next get(),
so a reallocation does not invalidate a pointer still in use.

No change to the database format or the query API.

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

Copy link
Copy Markdown
Owner Author

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

Enforce an invariant after a batch of steps, not at every step along the way. Instead of using a set where the invariant is true at all times (but at the cost of tree rebalancing), use a vector, then sort+uniq it once in bulk.

cldellow commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner Author

Re-measured independently on a US northeast full-history extract (Geofabrik), base upstream/master a9d8f15. I wrote the same change again before finding this PR: a sorted std::vector, stable-sorted and de-duplicated keeping the first of equivalent entries, with lookups by std::lower_bound. So these numbers are for an equivalent patch, not this branch's exact commit.

Instruction counts (valgrind callgrind; these repeat exactly between runs of the same build):

Query master this PR change
p13 1.801 G 1.741 G -3.34%
s_m1_meta_amenity_small 3.239 G 2.656 G -18.02%
s_m3_attic_meta_small 4.109 G 4.044 G -1.59%

CPU time, warm cache, 4-core VM, builds alternating, median of 3 (noise about ±4% per query):

Query master this PR change
m1 1.41 s 1.24 s -12%
m2 1.24 s 1.16 s -6%
m3 3.00 s 3.07 s +2%
p07 6.11 s 5.75 s -6%
p13 0.23 s 0.22 s -4%
total 12.0 s 11.4 s -4.6%

The gain is largest for out meta over many current elements (−18% instructions on s_m1). At a past date (s_m3) it is smaller (−1.6%). p07 and p13 are two of the 20 heaviest query templates on the public servers in June 2026, moved to New England coordinates. Output is byte-identical on all of them.

One detail I noticed while writing it: reset() doesn't clear current_objects, so entries from before a reset stay alongside the new ones, with the std::set as well as with a vector that is sorted and de-duplicated as a whole. Both keep the same contents.

Small queries for instruction counts

s_d1_adiff_small

[adiff:"2025-01-01T00:00:00Z","2026-11-01T00:00:00Z"]; (node["amenity"](42.34,-71.10,42.37,-71.04); way["building"](42.35,-71.07,42.36,-71.05); ); out geom;

s_d2_diff_highways_small

[diff:"2024-01-01T00:00:00Z","2026-11-01T00:00:00Z"]; way["highway"](42.34,-71.08,42.36,-71.05); out geom;

s_d4_diff_convert_small

[diff:"2024-01-01T00:00:00Z","2026-11-01T00:00:00Z"]; nwr["amenity"](42.34,-71.08,42.36,-71.05); convert item ::id=id(), name=t["name"], amenity=t["amenity"]; out;

s_m1_meta_amenity_small

node["amenity"](42.3,-71.15,42.4,-71.0); out meta;

s_m3_attic_meta_small

[date:"2022-01-01T00:00:00Z"]; way["highway"](42.34,-71.08,42.36,-71.05); out meta;
Meta queries m1–m3

m1_meta_amenity

node["amenity"](42.2,-71.3,42.5,-70.9); out meta;

m2_meta_buildings

way["building"](42.3,-71.15,42.4,-71.0); out meta;

m3_attic_meta_highways

[date:"2022-01-01T00:00:00Z"]; way["highway"](42.3,-71.2,42.4,-71.0); out meta;
p07 and p13
(rel['seamark:type']; >; way['seamark:type']; >; node['seamark:type']; ); out meta;
[bbox:42.355,-71.065,42.365,-71.055]; (node(42.355,-71.065,42.365,-71.055); way(bn); node(w); ); (._; (rel(bn)->.a; rel(bw)->.a; ); rel(br); ); out meta;

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.

Metadata lookups spend much of their time maintaining a std::set

2 participants