Skip to content

perf(frag-reuse): pick the compact remap's resident bitmap encoding for lookup cost - #45

Merged
amunra merged 1 commit into
release-8.0.0from
amunra/frag-reuse-resident-bitmap-encoding
Aug 21, 2026
Merged

amunra merged 1 commit into
release-8.0.0from
amunra/frag-reuse-resident-bitmap-encoding

Conversation

@amunra

@amunra amunra commented Aug 21, 2026

Copy link
Copy Markdown

The problem

CompactRowAddrRemap::get reaches RoaringBitmap::rank, which sums len() over every
container 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::new cloned the deserialized payload's bitmaps exactly as they arrived, so it
inherited 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 this
structure 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 the
wrong 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:

rows containers before after
256K 2 238 ns 154 ns
1M 8 457 ns 199 ns
4M 31 1161 ns 236 ns
16M 123 3678 ns 282 ns

Per-lookup cost grew with row count before and is flat after.

Two things to keep the claim honest:

  • At a low hit rate the gain is much smaller. A miss returns before any rank, so where
    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.
  • Payloads with no deletion holes keep run encoding and are unaffected.

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:

  • resident bitmaps carry no run containers when runs are dense
  • run encoding is kept when runs are few
  • no run containers needs no special case — the unguarded byte subtraction

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

…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]>
@amunra
amunra merged commit d644125 into release-8.0.0 Aug 21, 2026
1 check passed
@amunra
amunra deleted the amunra/frag-reuse-resident-bitmap-encoding branch August 21, 2026 15:07
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants