Stream L0 index metadata instead of collecting and sorting
perfloop/neon · Inefficient Algorithm
https://perfloop.ai/t/oss/case_bn7ej53pyt
Verdict
VERIFIED · settled 2026-11-14 · pull request opened as neondatabase/neon#12937
What happened: The paired measurements met the required improvement.
Hypothesis
A static source trace of `compact_level0_phase1` found that every selected layer calls `index_entries`, each returned vector is extended into `all_keys`, and the aggregate is globally `sort_by_key` before it is retained for `all_keys_iter` during output writing. The direct-callee source check of `DeltaLayerInner::index_entries` showed a forward visit of the entire on-disk B-tree into a `Vec<DeltaEntry>` per layer. The target’s own cancellation comment documents benchmark-scale layers of roughly a million keys, so N is the total key/LSN-entry cardinality across a selected contiguous L0 batch; the byte selection cap does not make N a small constant. A separate source check showed the value side already uses a heap-based `MergeIterator`, making an ordered index cursor a plausible way to preserve the metadata consumers rather than treating that work as unused. The sort comment notes that sorted subranges are fast, so this does not assume worst-case sort behavior or claim an end-to-end bottleneck. No runtime CPU, allocation, or latency measurement was run; the possible saving is the batch-wide metadata materialization and sort, at the real cadence of one background `compaction_loop` L0 batch whenever the threshold is met. Confirm with a reproducible L0-compaction benchmark sweeping both selected-layer count and entries per layer through the documented million-key scale, comparing elapsed phase time (including the emitted `read_lock_held_key_sort_micros` field), allocation bytes/peak RSS, and a CPU profile attributed to index collection/sorting. Also differentially verify identical compacted values, hole choices, duplicate-LSN splits, and output layer descriptors.
Change to test: For each threshold-triggered background L0 compaction batch, replace the batch-wide `all_keys` collection and `sort_by_key` with a bounded k-way cursor over the selected delta-layer B-tree indexes. Feed the existing hole selection and key-size/duplicate-LSN boundary decisions from that ordered cursor while the value merge writes output, preserving hole ranking, duplicate-key splits, and output-layer layout without retaining every `DeltaEntry` for the whole batch.
Where it lives
perfloop/neon · pageserver/src/tenant/tasks.rs
Evidence
Overlapping L0 compaction · 9 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
peak_rss_kib |
1203940 |
497872 |
−58.5% (−704112) |
−730500 to −607500 |
gain > 0; gain > 0.05 × |baseline median|; arm medians separated |
PASSED |
elapsed_us |
11216194 |
9755638 |
−13.1% (−1473871) |
−1579425 to −1389458 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocation_bytes |
10832061229 |
7656831870 |
−29.3% (−3175229587) |
−3175248340 to −3175221103 |
loss ≤ 0.05 × |baseline median| |
PASSED |
Disjoint L0 compaction · 11 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
peak_rss_kib |
1263992 |
479692 |
−62.6% (−790732) |
−805568 to −757064 |
loss ≤ 0.05 × |baseline median| |
PASSED |
elapsed_us |
9940082 |
8141447 |
−18.3% (−1815752) |
−2039290 to −1713866 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocation_bytes |
8404079376 |
6656578112 |
−20.8% (−1747533518) |
−1747629847 to −1747478834 |
loss ≤ 0.05 × |baseline median| |
PASSED |
Minimal L0 compaction · 29 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
peak_rss_kib |
102078 |
102528 |
+506 |
−64 to +812 |
loss ≤ 0.05 × |baseline median| |
PASSED |
elapsed_us |
1891 |
1925 |
+23.5 |
−29.5 to +81 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocation_bytes |
1581495 |
1581508 |
+13 |
−1907 to +1932 |
loss ≤ 0.05 × |baseline median| |
PASSED |
Tiny L0 compaction · 318 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
peak_rss_kib |
151245 |
151703 |
+0.3% (+447) |
+308 to +576 |
loss ≤ 0.05 × |baseline median| |
PASSED |
elapsed_us |
3536 |
3565 |
+22.5 |
−151 to +169.5 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocation_bytes |
2460431 |
2459395 |
−259.3 |
−1984 to +807.5 |
loss ≤ 0.05 × |baseline median| |
PASSED |
Small L0 compaction · 13 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
peak_rss_kib |
151736 |
152346 |
+470 |
−736 to +1368 |
loss ≤ 0.05 × |baseline median| |
PASSED |
elapsed_us |
13401 |
13196 |
−244 |
−906 to +627.5 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocation_bytes |
13566259 |
12928937 |
−4.7% (−638251) |
−660341 to −611388 |
loss ≤ 0.05 × |baseline median| |
PASSED |
Adaptive L0 compaction · 14 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
peak_rss_kib |
631922 |
498404 |
−20.5% (−129800) |
−146764 to −92196 |
loss ≤ 0.05 × |baseline median| |
PASSED |
elapsed_us |
4498574 |
4251595 |
−5.4% (−243901) |
−335979 to −167727 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocation_bytes |
4452136869 |
4230134370 |
−5% (−222001798) |
−222029033 to −221990653 |
loss ≤ 0.05 × |baseline median| |
PASSED |
Wide adaptive L0 compaction · 14 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
peak_rss_kib |
628540 |
500730 |
−20.3% (−127810) |
−136412 to −114524 |
loss ≤ 0.05 × |baseline median| |
PASSED |
elapsed_us |
4936711 |
4645622 |
−5.7% (−281158) |
−378380 to −181800 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocation_bytes |
4825126715 |
4599924210 |
−4.7% (−225201645) |
−225220690 to −225193494 |
loss ≤ 0.05 × |baseline median| |
PASSED |
Wide streaming L0 compaction · 15 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
peak_rss_kib |
1551752 |
568284 |
−63.8% (−989928) |
−1122816 to −845644 |
loss ≤ 0.05 × |baseline median| |
PASSED |
elapsed_us |
17805066 |
16052367 |
−10.2% (−1814630) |
−2082833 to −1540158 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocation_bytes |
16299635358 |
12250815061 |
−24.8% (−4048820983) |
−4048844340 to −4048667862 |
loss ≤ 0.05 × |baseline median| |
PASSED |
Wide materialized L0 compaction · 15 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
peak_rss_kib |
1024832 |
765804 |
−25.4% (−259988) |
−280356 to −246148 |
loss ≤ 0.05 × |baseline median| |
PASSED |
elapsed_us |
7923307 |
7450030 |
−6.1% (−484621) |
−647556 to −386032 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocation_bytes |
7825010561 |
7375800842 |
−5.7% (−449211348) |
−449269906 to −449159694 |
loss ≤ 0.05 × |baseline median| |
PASSED |
Checks: 1 of 1 passed. Verification: no defect found.
Timeline
2026-07-21· Case opened2026-08-25· PR opened