Skip to content

BSI: add Retain for in-place filtering#517

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

BSI: add Retain for in-place filtering#517
lemire merged 2 commits intoRoaringBitmap:masterfrom
anacrolix:bsi-retain

Conversation

@anacrolix
Copy link
Contributor

Summary

Retain is the in-place equivalent of NewBSIRetainSet: it removes all
values whose column IDs are not in the provided bitmap, modifying the BSI
in place and returning the number of column IDs dropped.

Prefer Retain over NewBSIRetainSet when no copy is needed — for
example when the BSI will be immediately re-serialized — as it avoids
allocating a new BSI and cloning all bit planes.

The implementation uses an optimization: if the existence bitmap is
unchanged after the intersection (i.e. retain covers all existing column
IDs), the bit planes are skipped entirely. This is correct because BSI
consistency guarantees that bA contains no set bits for column IDs
absent from eBM.

A test is included covering both the drop path and the no-op early-return.

Expand the doc comment to explain: in-place semantics vs NewBSIRetainSet,
why skipping bA when dropped==0 is correct (BSI consistency invariant), and
when to prefer Retain over the allocating alternative.

The in-place form is used in caterwaul's CleanBitmaps, which iterates every
term bitmap and filters it down to currently-valid doc IDs before writing it
back to storage. No new BSI allocation is needed since the result is
immediately serialized.
@anacrolix anacrolix marked this pull request as ready for review March 9, 2026 11:01
@lemire lemire merged commit 3a8d6b5 into RoaringBitmap:master Mar 9, 2026
8 checks passed
@anacrolix
Copy link
Contributor Author

Cheers

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