Quadratic tombstone-union work in sorted compaction
perfloop/weaviate · Inefficient Algorithm
https://perfloop.ai/t/oss/case_y46xv2nvy0
Verdict
VERIFIED · settled 2026-11-13
What happened: The paired measurements met the required improvement.
Hypothesis
Source inspection of `writeNodeCommits` showed that Phase 2 appends beyond-array `Tombstones` IDs, linearly scans that slice for every beyond-array `TombstonesDeleted` ID, and then insertion-sorts the combined slice. I ran `go test ./adapters/repos/db/vector/hnsw/compact -run '^(TestSortedWriter_TombstoneBeyondNodesArray|TestSortedWriter_RemoveTombstoneBeyondNodesArray|TestSortedWriter_CombinedScenario)$' -count=1`; it passed and confirms round-trip preservation of beyond-array add/remove tombstones and the combined case, but it did not measure timing.
Let A and B be the beyond-array counts in the two maps and K their unique union. The duplicate scans can take O(B(A+B)) comparisons and insertion sort can take O(K²) comparisons and moves; `GraphState` places no cardinality cap on either map. On the compactor path, this runs once per converted file during commit-log maintenance, before output; that path uses a buffered safe-file writer and each tombstone record is nine bytes. Large beyond-array tombstone sets are therefore a plausible CPU cost, but this hunt did not measure their production cardinality or phase share.
Replace the slice membership scan with a set-based union and sort its materialized keys with `sort.Slice`; the removed delta is the repeated linear duplicate search and quadratic insertion shifts. A case should benchmark `SortedWriter.WriteAll` or conversion across a K sweep with add-only, remove-only, overlapping, randomized, and reverse-ordered IDs beyond `Nodes`, and collect a CPU profile showing reduced Phase 2 time. It should also round-trip those cases and verify sorted IDs plus add/remove cancellation semantics.
Change to test: Build the beyond-array tombstone ID union with a set, materialize it once, and use the standard library sorter. Keep writeTombstoneOps as the single cancellation decision so output semantics remain unchanged.
Where it lives
perfloop/weaviate · adapters/repos/db/vector/hnsw/commit_logger.go
Evidence
100k-delete commit-log conversion · 14 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
1220186550 |
40279234 |
−96.7% (−1180118017) |
−1196507455 to −1161763299 |
gain > 0; gain > 0.05 × |baseline median|; arm medians separated |
PASSED |
B/op |
13109772 |
13107555 |
−2449 |
−7244 to +2090 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
300685 |
300683 |
−2 |
−3 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
1k-delete commit-log conversion · 15 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
4740368 |
4741357 |
+281 |
−117315 to +134531 |
loss ≤ 0.05 × |baseline median| |
PASSED |
B/op |
1176988 |
1176954 |
−50 |
−328 to +230 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
3145 |
3145 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
insert-only commit-log conversion · 15 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
57115031 |
56169845 |
−937418 |
−3126243 to +2589898 |
loss ≤ 0.05 × |baseline median| |
PASSED |
B/op |
13820575 |
13820053 |
−190 |
−3331 to +1217 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
965121 |
965120 |
0 |
−1 to +1 |
loss ≤ 0.05 × |baseline median| |
PASSED |
cleanup-only commit-log conversion · 25 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
323900220 |
313282121 |
−2939940 |
−17464878 to +13607756 |
loss ≤ 0.05 × |baseline median| |
PASSED |
B/op |
303187373 |
303187546 |
−280 |
−3722 to +2432 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
3157507 |
3157507 |
0 |
−3 to +2 |
loss ≤ 0.05 × |baseline median| |
PASSED |
delete-and-cleanup commit-log conversion · 34 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
385685076 |
380738641 |
−3727107 |
−25481140 to +14733546 |
loss ≤ 0.05 × |baseline median| |
PASSED |
B/op |
349713683 |
349713060 |
−909 |
−2743 to +2208 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
3556640 |
3556639 |
−1 |
−2 to +1 |
loss ≤ 0.05 × |baseline median| |
PASSED |
update commit-log conversion · 45 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
226110001 |
228619922 |
+968825 |
−9106908 to +10499548 |
loss ≤ 0.05 × |baseline median| |
PASSED |
B/op |
59848953 |
59849003 |
−924 |
−10723 to +8219 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
4618755 |
4618755 |
0 |
−2 to +1 |
loss ≤ 0.05 × |baseline median| |
PASSED |
Checks: 3 of 3 passed. Verification: no defect found.
Timeline
2026-08-12· Case opened