Repository navigation
perf(frag-reuse): pick the compact remap's resident bitmap encoding for lookup cost - #45
Merged
Conversation
…or lookup cost `CompactRowAddrRemap::get` reaches `RoaringBitmap::rank`, which sums `len()` over every container below the target. For bitmap and array containers `len()` is O(1) -- a cached field and a `Vec::len` respectively -- but for run containers it rescans the interval list. `GroupRemap::new` cloned the deserialized payload's bitmaps as they arrived, so it inherited whatever encoding was on disk, and compaction deliberately run-optimizes the address set before writing it. `optimize` chooses by serialized size, which is the right objective for a payload and the wrong one for a structure that will be ranked. Where the address set has deletion holes -- many short runs -- inheriting that encoding makes each lookup O(#containers x #runs), so it scales with fragment width in rows, when this structure exists to be bounded by fragment count. Stripping unconditionally is not the answer either: a fragment rewritten whole is one contiguous run per container, which ranks as cheaply as a cached read and costs six bytes against a bitmap container's fixed 8 KiB. So the encoding is now chosen per bitmap by run density, keeping whichever is cheaper to rank. The serialized payload is untouched, so the on-disk win that motivated run-optimizing is unaffected. Where stripping happens the resident bitmaps grow -- bounded by fragment count, and confined to the case where run encoding was the bulkier option anyway. `MAX_RUNS_PER_CONTAINER` sits at the low end deliberately. The cheap regime is one run per container, and the error is asymmetric: keeping runs that should be stripped costs a term that grows with the dataset, while stripping runs that should be kept costs a small constant. The size of the win depends on how often lookups hit the reuse index at all, since a miss returns before any rank happens: substantial on hit-heavy loads over wide fragments, a smaller constant factor where most probed addresses fall outside the index's fragments. Two tests, one per branch. Each asserts the payload arrives run-encoded before checking the outcome, so neither can pass vacuously, and that the choice moves no address. Both were verified to fail under the opposite mutation. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
andrea-reale
approved these changes
Aug 21, 2026
This was referenced Aug 28, 2026
amunra
added a commit
that referenced
this pull request
Sep 2, 2026
…or lookup cost (#45) `CompactRowAddrRemap::get` reaches `RoaringBitmap::rank`, which sums `len()` over every container below the target. For bitmap and array containers `len()` is O(1) -- a cached field and a `Vec::len` respectively -- but for run containers it rescans the interval list. `GroupRemap::new` cloned the deserialized payload's bitmaps as they arrived, so it inherited whatever encoding was on disk, and compaction deliberately run-optimizes the address set before writing it. `optimize` chooses by serialized size, which is the right objective for a payload and the wrong one for a structure that will be ranked. Where the address set has deletion holes -- many short runs -- inheriting that encoding makes each lookup O(#containers x #runs), so it scales with fragment width in rows, when this structure exists to be bounded by fragment count. Stripping unconditionally is not the answer either: a fragment rewritten whole is one contiguous run per container, which ranks as cheaply as a cached read and costs six bytes against a bitmap container's fixed 8 KiB. So the encoding is now chosen per bitmap by run density, keeping whichever is cheaper to rank. The serialized payload is untouched, so the on-disk win that motivated run-optimizing is unaffected. Where stripping happens the resident bitmaps grow -- bounded by fragment count, and confined to the case where run encoding was the bulkier option anyway. `MAX_RUNS_PER_CONTAINER` sits at the low end deliberately. The cheap regime is one run per container, and the error is asymmetric: keeping runs that should be stripped costs a term that grows with the dataset, while stripping runs that should be kept costs a small constant. The size of the win depends on how often lookups hit the reuse index at all, since a miss returns before any rank happens: substantial on hit-heavy loads over wide fragments, a smaller constant factor where most probed addresses fall outside the index's fragments. Two tests, one per branch. Each asserts the payload arrives run-encoded before checking the outcome, so neither can pass vacuously, and that the choice moves no address. Both were verified to fail under the opposite mutation. Co-authored-by: Claude Opus 5 (1M context) <[email protected]> (cherry picked from commit d644125)
amunra
added a commit
that referenced
this pull request
Sep 2, 2026
… rank (#69) * Run-optimize the compaction row-address set before writing it (#39) The fragment-reuse payload records which row addresses a compaction moved, as a serialized RoaringTreemap. Compaction rewrites long contiguous spans, but the treemap is written without ever calling `optimize()`, so those spans are stored as dense bitmap containers at one bit per row. On a real payload here that is 88 MB describing 676M addresses, essentially all of it bitmap containers (676M bits is 84.6 MB). The same information as run containers is a few bytes per span. The payload is read on every index open, so this shrinks both the object and the deserialize that follows it. No format change: run containers are already produced today by the binary-copy path, which reaches `Store::Run` through `insert_range`. Co-authored-by: Claude Opus 5 (1M context) <[email protected]> (cherry picked from commit dec9486) * test(optimize): assert the captured row-address payload is run-encoded The preceding commit run-optimizes the address set before serializing it, but shipped without a test. This asserts both the mechanism and the outcome: every per-fragment bitmap in the deserialized payload holds exactly one run container and nothing else, and the payload is more than 20x smaller than the same addresses re-inserted, which is the encoding they would carry unoptimized. Fragments hold 5,000 rows deliberately. Below roaring's 4096-element array limit an unoptimized store is an array container, not the dense bitset `optimize()` exists to replace, so a smaller fragment would exercise the wrong path. The size bound is computed in the test rather than hardcoded, so it states the guarantee instead of a magic number and survives changes to fragment count or roaring's thresholds. Verified the test is not vacuous: with `row_addrs.optimize()` removed it fails with `left: (0, 0, 1)` against `right: (1, 0, 0)` -- one 64 KiB dense bitset holding 5,000 values. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> * perf(frag-reuse): pick the compact remap's resident bitmap encoding for lookup cost (#45) `CompactRowAddrRemap::get` reaches `RoaringBitmap::rank`, which sums `len()` over every container below the target. For bitmap and array containers `len()` is O(1) -- a cached field and a `Vec::len` respectively -- but for run containers it rescans the interval list. `GroupRemap::new` cloned the deserialized payload's bitmaps as they arrived, so it inherited whatever encoding was on disk, and compaction deliberately run-optimizes the address set before writing it. `optimize` chooses by serialized size, which is the right objective for a payload and the wrong one for a structure that will be ranked. Where the address set has deletion holes -- many short runs -- inheriting that encoding makes each lookup O(#containers x #runs), so it scales with fragment width in rows, when this structure exists to be bounded by fragment count. Stripping unconditionally is not the answer either: a fragment rewritten whole is one contiguous run per container, which ranks as cheaply as a cached read and costs six bytes against a bitmap container's fixed 8 KiB. So the encoding is now chosen per bitmap by run density, keeping whichever is cheaper to rank. The serialized payload is untouched, so the on-disk win that motivated run-optimizing is unaffected. Where stripping happens the resident bitmaps grow -- bounded by fragment count, and confined to the case where run encoding was the bulkier option anyway. `MAX_RUNS_PER_CONTAINER` sits at the low end deliberately. The cheap regime is one run per container, and the error is asymmetric: keeping runs that should be stripped costs a term that grows with the dataset, while stripping runs that should be kept costs a small constant. The size of the win depends on how often lookups hit the reuse index at all, since a miss returns before any rank happens: substantial on hit-heavy loads over wide fragments, a smaller constant factor where most probed addresses fall outside the index's fragments. Two tests, one per branch. Each asserts the payload arrives run-encoded before checking the outcome, so neither can pass vacuously, and that the choice moves no address. Both were verified to fail under the opposite mutation. Co-authored-by: Claude Opus 5 (1M context) <[email protected]> (cherry picked from commit d644125) --------- Co-authored-by: Ryan Mack <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
CompactRowAddrRemap::getreachesRoaringBitmap::rank, which sumslen()over everycontainer below the target. That is O(1) for bitmap and array containers — a cached field
and a
Vec::len— but for run containers it rescans the interval list.GroupRemap::newcloned the deserialized payload's bitmaps exactly as they arrived, so itinherited whatever encoding was on disk. Compaction deliberately run-optimizes the address
set before writing it (#39). So where the set has deletion holes — many short runs — every
lookup became
O(#containers x #runs), scaling with fragment width in rows, when thisstructure exists to be bounded by fragment count.
The fix
Choose the resident encoding per bitmap by run density rather than inheriting it.
optimize()selects by serialized size, which is the right objective for a payload and thewrong one for a structure that will be ranked.
This is not a revert of #39. The serialized payload is untouched, so its on-disk win
stands. And stripping unconditionally would be wrong in the other direction: a fragment
rewritten whole is one contiguous run per container, which ranks as cheaply as a cached
read and costs six bytes against a bitmap container's fixed 8 KiB. Hence the per-bitmap
choice, with the threshold deliberately at the low end — the error is asymmetric, since
keeping runs that should be stripped costs a term that grows with the dataset while
stripping runs that should be kept costs a small constant.
Measurements
From a synthetic reuse chain 10-16 versions deep. The harness is not part of this PR, so
these are not reproducible from the tree yet; the mechanism above is verifiable from the
roaring source.
Every probe hitting the reuse index:
Per-lookup cost grew with row count before and is flat after.
Two things to keep the claim honest:
most probed addresses fall outside the reuse index's fragments — the common case for an
index over settled fragments — this is nearer 1.3x than 13x.
Where stripping does happen the resident bitmaps grow, bounded by fragment count, and
confined to the case where run encoding was the bulkier option anyway.
Tests
Three, covering both branches of the decision plus the degenerate case:
Each asserts the payload arrives run-encoded before checking the outcome, so none can pass
vacuously, and each checks the choice moves no address. All three were verified to fail under
the opposite mutation.
🤖 Generated with Claude Code