Skip to content

Filter bbox and past-date results in place, without vector slack - #130

Open
cldellow wants to merge 1 commit into
masterfrom
compact-filtered-results
Open

cldellow wants to merge 1 commit into
masterfrom
compact-filtered-results

Conversation

@cldellow

@cldellow cldellow commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Fixes #129.

Merge after #104. The code doesn't depend on #104, but the gain does. With #104's chunked past-date bbox collection, this lowers peak RSS on every query measured. On master alone, it lowers some and raises others by up to 15% (tables below).

Change

filter_by_bbox (data/bbox_filter.cc) and both loops of filter_attic_elements (data/collect_members.h) built each index's filtered vector in a new local vector by push_back, then swapped it in. They now compact the vector in place, keeping the order, then erase the tail and shrink_to_fit. A kept element is moved (or copied, for node coordinates) only when its slot changes, and filter_attic_elements reads the timestamps before the move.

Two files, 34 lines. This doesn't change the query API, the on-disk format or [maxsize:] accounting. Output is unchanged.

Bound: no new allocation except shrink_to_fit's exact-size copy of each kept vector, which replaces the old push_back-grown copy (up to twice its size).

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;
R2 plain [out:json]; way[highway]; out geom;
R3 plain [out:xml]; rel(42,-67.6,47.5,-58); out body;
R4 plain [out:json]; nwr[name]; out center;
R7 history [date:"2018-01-01T00:00:00Z"][out:csv(::count)]; node(42,-67.6,47.5,-58); out count;
R9 history [date:"2018-01-01T00:00:00Z"][out:json]; way[highway]; out geom;
R10 history [date:"2018-01-01T00:00:00Z"][out:json]; nwr[name]; out center;
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;

On all-changes (at 7b7d798, which includes #104), without and with this commit

One run of each under GNU time.

Query CPU before CPU after RSS before RSS after
R18 6.27 s 6.31 s 597.6 MB 560.2 MB (−6%)
R17 5.58 s 5.38 s 342.6 MB 330.2 MB (−4%)
R7 4.30 s 3.81 s 311.0 MB 279.4 MB (−10%)
R21 20.91 s 20.96 s 817.5 MB 745.2 MB (−9%)
P2 18.84 s 19.19 s 843.6 MB 759.2 MB (−10%)
P1 3.25 s 3.47 s 599.2 MB 591.6 MB
R20 2.11 s 2.11 s 92.8 MB 93.2 MB
R2 3.09 s 3.17 s 176.2 MB 176.6 MB

Against master

CPU is the median of 5 (plain) or 3 (history) alternating runs (bench/compare.py --no-cache); peak RSS is from one extra run under GNU time (--rss). R21 is one run of each under GNU time, on top of #128, because on master its CPU is dominated by the quadratic pass that #126 and #128 remove.

Query CPU B/A RSS master RSS this PR
P2 1.03 824.6 MB 742.1 MB (−10%)
R17 (3 runs) 1.01 1,316.6 MB 1,214.2 MB (−8%)
R7 (3 runs) 1.02 1,242.5 MB 1,145.2 MB (−8%)
R18 (3 runs) 1.01 1,172.8 MB 1,349.1 MB (+15%)
R21 (1 run, with #128) 1.13 1,271.5 MB 1,407.9 MB (+11%)
P1 0.94 683.3 MB 673.6 MB
R2 0.97 287.1 MB 287.3 MB
R3 1.02 329.6 MB 330.0 MB
R4 0.99 262.1 MB 262.3 MB
R9 (3 runs) 0.94 235.9 MB 235.7 MB
R10 (3 runs) 1.00 265.7 MB 265.9 MB
R13 (3 runs) 1.00 324.3 MB 324.3 MB
R20 (3 runs) 1.04 107.8 MB 107.7 MB

Benchmark queries q01–q12 (7 runs): total CPU ×1.00, RSS within 1.6 MB on each. History benchmark h1–h6 (5 runs): total ×1.02, RSS within 8.3 MB on each, none higher by more than 0.3 MB. The queries are defined in #2 and #12.

Why R18 and R21 go up on master: heaptrack on R18 shows the heap peak falls, from 1.11 GB to 1.01 GB, but RSS rises from 1.23 GB to 1.42 GB (heaptrack overhead included). That is fragmentation, not live data. On master, a past-date bbox query collects all candidates of the box at once, current and attic versions, then filters them index by index. Each shrink_to_fit allocates the exact-size copy while the large vector is alive, and the freed large blocks leave holes that the later, smaller allocations don't fill. With #104, the candidates come in chunks that are freed and reused, and this doesn't happen.

Two variants were tried on master, one run each: without shrink_to_fit, and shrinking only when at least half the capacity is slack. Both remove the R18 regression but also all of the gains (R17 1,375 / 1,343 MB, R7 1,289 / 1,269 MB, P2 839 / 839 MB, against 1,348, 1,272 and 845 MB on master).

Testing

  • Output was byte-identical on every query above, against master and on all-changes.
  • CI, against upstream/master (8edf11b), seed 1, no differences:
    • fuzz run 45112555705: plain 1,990 same and 10 skipped, areas,complete 1,999 and 1, evaluators 1,998 and 2. Skipped means a timeout or resource limit, which isn't compared.
    • history run 45109177980, history,users,complete: 998 same, 2 skipped.
    • props run 45109197296, 60 seeds: no new failures. The 9 checks that fail also fail on upstream/master.
  • Mutation: the mutant for this change on all-changes (0ec8fa9 in Mutants for the memory commits on all-changes #153) is caught by 18 property checks.

Limitations

🤖 Generated with Claude Code

https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm

filter_by_bbox and filter_attic_elements built each index's filtered
result in a new vector by push_back and swapped it in. The kept vector
carried up to twice its size in slack from doubling, and the input
vector, itself grown by push_back, briefly existed alongside it.

Compact each index's vector in place instead, keeping the order, then
shrink it to its size. Kept elements are moved only when their slot
changes, so ways and relations no longer copy their member vectors when
nothing is removed. Output is unchanged.

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.

filter_by_bbox and filter_attic_elements rebuild each result vector by push_back, up to 2.2× its size

2 participants