Skip to content

print: reference id-sorted elements in 8 bytes - #150

Open
cldellow wants to merge 2 commits into
print-reserve-refsfrom
print-compact-refs
Open

cldellow wants to merge 2 commits into
print-reserve-refsfrom
print-compact-refs

Conversation

@cldellow

@cldellow cldellow commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #121 (print-reserve-refs), which it targets: it replaces the reference vector #121 reserves. Review and merge #121 first.

Fixes #149.

Change

To print in id order (every out mode except qt), print.cc sorts one reference per element by id. A reference was a pointer and the element's index, 16 bytes. With attic elements it was a Maybe_Attic_Ref of index, pointer and timestamp, 24 bytes.

A new class Items_By_Id in print.cc replaces both collect_items_by_id overloads, Maybe_Attic_Ref and Skeleton_Comparator_By_Id:

  • It keeps a table with one entry per non-empty vector of the set, current vectors first. Each entry holds the address of the vector's first element (its Object part), the size of its elements (sizeof(Object) or sizeof(Attic< Object >)) and its index.
  • A reference is the entry number and the position in the vector, two uint32, 8 bytes. The object is at the entry's address plus position times element size, so current and attic elements are found the same way, with no branch in the sort's comparisons.
  • The index comes from the entry. An attic element's timestamp comes from the Attic element itself. timestamp() returns NOW for current entries, so the existing == NOW tests are unchanged.
  • References are appended in the same order as before (current, then attic, each in map order) and sorted with the same std::sort by id. Elements with equal ids therefore keep the same order.

The callers, by_id (both overloads), tags_by_id and tags_by_id_attic, read through obj(i), idx(i) and timestamp(i).

Two commits: the first uses two typed tables and branches on the kind of element. The second merges them into one table, which removed most of the first commit's CPU cost (table below). One file, print.cc. This doesn't change the query API, the on-disk format or [maxsize:] accounting (the references were never counted). Output is unchanged.

Bound: 8 bytes per element, instead of 16 (current only) or 24 (with attic elements). Plus 16 bytes per non-empty index in the table: about 140K entries, 2.2 MB, for the whole-province node queries below.

Impact

Warm cache, 4-core cloud VM with 15 GB RAM. Outputs are byte-identical everywhere. Databases: the Geofabrik Nova Scotia extract of 2026-11-08 imported with --meta (plain), and a Nova Scotia full-history extract (2006 to 2026-11-15) imported with --keep-attic by a better-imports build (history). Both use lz4 compression.

Queries

Name Database Query
P1 plain [out:csv(::id;false)]; node(42,-67.6,47.5,-58); out ids;
P2 plain [out:xml]; node(42,-67.6,47.5,-58); out;
W1 plain [out:xml]; way(42,-67.6,47.5,-58); out;
R3 plain [out:xml]; rel(42,-67.6,47.5,-58); out body;
R7 history [date:"2018-01-01T00:00:00Z"][out:csv(::count)]; node(42,-67.6,47.5,-58); out count;
R13 history [date:"2018-01-01T00:00:00Z"][out:xml]; rel(42,-67.6,47.5,-58); out body;
R17 history [date:"2012-01-01T00:00:00Z"][out:csv(::id;false)]; node(42,-67.6,47.5,-58); out ids;
R18 history [date:"2021-03-01T00:00:00Z"][out:csv(::id;false)]; node(42,-67.6,47.5,-58); out ids;
R20 history [date:"2018-01-01T00:00:00Z"][out:json]; way[highway](44.4,-64.0,45.0,-63.0); out geom;
R21 history [date:"2018-01-01T00:00:00Z"][out:xml]; node(42,-67.6,47.5,-58); out;
W2 history [date:"2018-01-01T00:00:00Z"][out:xml]; way(42,-67.6,47.5,-58); out meta;

On all-changes (at 1ef6664 plus #140), without and with this PR

This is where the large outputs peak in the output stage. RSS is one run of each under GNU time. CPU is the median of 7 alternating runs (bench/compare.py --no-cache), for the first commit alone and for both.

Query RSS before RSS after CPU, first commit CPU, both commits
R18 560.1 MB 385.2 MB (−31%) ×1.10 ×1.07
R21 518.5 MB 363.4 MB (−30%) ×1.01 ×0.98
P1 591.7 MB 399.1 MB (−33%) ×1.10 ×1.01
R17 329.8 MB 268.7 MB (−19%) ×1.02 ×1.04
P2 537.2 MB 441.0 MB (−18%)
W1 464.1 MB 459.5 MB
W2 454.3 MB 447.3 MB
R3 198.9 MB 198.8 MB
R13 200.8 MB 200.6 MB
R7 279.3 MB 279.4 MB

Against #121

CPU is the median of alternating runs (bench/compare.py --no-cache): 5 for these queries, 7 for q01–q12, 5 for h1–h6. Peak RSS is from one extra run under GNU time (--rss).

Query CPU B/A RSS #121 RSS this PR
P1 1.05 585.3 MB 399.2 MB
P2 1.01 823.9 MB 730.4 MB
W1 0.95 1,092.9 MB 1,093.3 MB
R17 1.00 1,316.8 MB 1,317.1 MB
R18 1.04 1,172.5 MB 1,172.9 MB
R20 1.09 107.8 MB 107.7 MB
R21 1.08 1,242.8 MB 1,243.0 MB

Testing

Limitations

  • CPU: the sort reads each element through the table, one extra memory access per comparison. For 11.5M nodes at a past date (R18) this costs about 7% of the query's CPU on all-changes, about 0.4 s. For current-only output (P1) it is within noise.
  • Pointer arithmetic: the element lookup does pointer arithmetic on const char* over the vector's elements and casts back to const Object*. That relies on each element's Object part being at the same offset, which holds since Attic< Object > derives from Object without virtual bases.
  • Limits: positions and entry numbers are 32-bit. A single index vector or a set with more than 4G non-empty indexes would overflow. Neither is reachable within memory limits.

🤖 Generated with Claude Code

https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm

To print in id order, every out mode except qt puts one reference per
element in a vector and sorts it. A reference was a pointer and the
element's index, 16 bytes, or 24 when the set has attic elements, which
also carried the element's timestamp. For an out of 11.5M nodes at a
past date that was 277 MB.

Reference each element instead by the number of its index's entry in a
table of the set's non-empty vectors, current ones first, and its
position in that vector: two 32-bit numbers. The index, the object and
the timestamp of an attic element are read from the table and the set.
The references are filled in the same order as before and sorted with
the same std::sort by id, so elements with equal ids stay in the same
order. Output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm
The 8-byte references kept current and attic vectors in two tables, so
every comparison in the sort branched on the kind of element. Keep one
table instead, with each vector's first element (its Object part) and
the size of its elements, and find an element by its position times
that size. On a whole-province out ids of 12.6M current nodes this
takes the sort's extra cost from about 10% of the query's CPU to about
1%, and to about 7% for 11.5M nodes at a past date.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm
@cldellow cldellow added the query-memory label Nov 22, 2026 — with Claude
cldellow added a commit that referenced this pull request Nov 23, 2026
One mutant for each of nine all-changes commits from the memory work:
the past-date bbox chunking and its size check (#104), element counting
in history collection (#87), the reserved and the 8-byte output
references (#121, #150), in-place filtering (#130) and the Tag_Store
changes (#128, #140). Each old text occurs once in all-changes at
686a701.

The mutate workflow (run 45106469415, seeds 1-40) caught five: 1b30bbb,
0ec8fa9, 7b11467, f7274dc and 686a701. The other four are expected not
to be caught, as their notes say: 7d39a64 and 1ef6664 change only
memory, and 180c620 and 3f7cfa8 matter only for a query over
[maxsize:].

Part of #145.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants