Skip to content

BSI: add FromBitmaps#516

Merged
lemire merged 2 commits intoRoaringBitmap:masterfrom
anacrolix:bsi-frombitmaps
Mar 9, 2026
Merged

BSI: add FromBitmaps#516
lemire merged 2 commits intoRoaringBitmap:masterfrom
anacrolix:bsi-frombitmaps

Conversation

@anacrolix
Copy link
Contributor

Summary

FromBitmaps initializes a BSI from a pre-built slice of bitmaps, where
bms[0] is the existence bitmap and bms[1:] are the bit planes in
least-to-most-significant order — matching the layout of
MarshalBinary/UnmarshalBinary.

The caller transfers ownership of the slice; the BSI aliases it directly
without copying. This is intentional: the primary use case is a
deserialization pipeline where the existence bitmap is not stored on
disk
(it is reconstructed by ORing the bit planes on read, saving storage
space), and all bitmaps are freshly allocated from the stream. Copying at
that point would be pure waste. Because the caller's slice goes out of
scope immediately after the call, aliasing is safe.

A bounds check (panic if len(bms) < 1) is included since an empty slice
would cause a silent panic or corrupt state.

Add a bounds check (panic if len(bms) < 1) and a doc comment explaining
the ownership-transfer contract and why no-copy is intentional.

The primary caller (bsiDocFreqs.ReadFrom in caterwaul) reads bitmaps
freshly from a stream, reconstructs the existence bitmap by ORing the bit
planes (the eBM is not stored on disk to save space), then hands the whole
slice to FromBitmaps before it goes out of scope. All bitmaps are newly
allocated from the stream at that point, so copying would be pure waste.
The design also leaves the door open for a future zero-copy path once
roaring gains FromUnsafeBytes support.
@anacrolix anacrolix marked this pull request as ready for review March 9, 2026 11:02
@lemire lemire merged commit 584d599 into RoaringBitmap:master Mar 9, 2026
8 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.

2 participants