Skip to content

Never keep a run container with more than 2047 runs - #607

Merged
lemire merged 1 commit into
masterfrom
run-container-max-runs
Dec 2, 2026
Merged

lemire merged 1 commit into
masterfrom
run-container-max-runs

Conversation

@lemire

@lemire lemire commented Dec 2, 2026

Copy link
Copy Markdown
Member

A run container with 2048 runs or more serializes to more than the 8192 bytes of a bitmap container. CRoaring and Java never produce one, but this library could:

  • runContainer16.or (run ∪ run), iaddRange, iremoveRange and not returned run containers without any size check. For example, removing every other value of a full container with RemoveRange built a run container with 32768 runs (about 128 KiB instead of 8 KiB).
  • toEfficientContainer (run, array and bitmap versions) compared against bitmapContainerSizeInBytes(), which is 8224 bytes because it includes in-memory overhead. As a result it kept run containers with up to 2055 runs.

Change

  • Add maxRunsInRunContainer = 2047 and (*runContainer16).limitRuns(). It is a length check and converts to an array container (≤ 4096 values) or a bitmap container only when the limit is exceeded.
  • Call limitRuns() from or (run case), iaddRange, iremoveRange and not. inot, ior, andNot and iandNot already go through toEfficientContainer.
  • toEfficientContainer / toEfficientContainerFromCardinality on run, array and bitmap containers no longer return a run container with more than 2047 runs.

Validate is unchanged: serialized bitmaps with more runs (written by earlier versions) are still accepted.

Tests

  • New runcontainer_maxruns_test.go covers the limit at the container level (or, ior, lazyOR, iaddRange, iadd, iremoveRange, iremove, not, inot, andNot, iandNot, and every toEfficientContainer) and at the bitmap level (Or, in-place Or, FastOr, AddRange, Add, Flip, RemoveRange). Several of these fail without the fix.
  • TestHash: three expected checksums (N=4095/4096/4097, gap=32, part 2) changed, because those bitmaps are now stored as bitmap containers instead of 2048-run containers. Checksum depends on container type.
  • TestRunOptimizeRecoversSpaceAfterRemoveRange (root and roaring64) relied on RemoveRange building a 32768-run container. It is now TestRemoveRangeDoesNotGrowRunsPastBitmapSize, which checks that this no longer happens.
  • TestBitmapContainerIAndNot dropped the result of iaddRange. That only worked while the receiver stayed a run container that grew in place.
  • TestBitmapContainerToEfficientContainerChoice: its reference rule now includes the run limit.

A run container with 2048 runs or more serializes to more than the 8192
bytes of a bitmap container, and the other Roaring implementations never
produce one. The Go library could: or, iaddRange, iremoveRange and not
returned run containers without checking their size, and
toEfficientContainer compared against bitmapContainerSizeInBytes()
(8224 bytes, including in-memory overhead), so it kept up to 2055 runs.

Add maxRunsInRunContainer (2047) and a cheap limitRuns check that turns
an oversized run container into an array or bitmap container. Apply it
to the operations that could grow a run container unchecked, and make
the run/array/bitmap toEfficientContainer functions refuse to return a
run container past the limit.

Update tests that relied on the old behavior: hardcoded checksums of
bitmaps that are now bitmap containers instead of 2048-run containers,
a test that grew a run container to 32768 runs with RemoveRange, and a
test that dropped the result of iaddRange.
@lemire
lemire merged commit f897cb9 into master Dec 2, 2026
14 checks passed
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.

1 participant