Skip to content

Add shrink_to_fit to RoaringBitmap and RoaringTreemap - #398

Open
emschwartz wants to merge 1 commit into
RoaringBitmap:mainfrom
emschwartz:shrink-to-fit
Open

emschwartz wants to merge 1 commit into
RoaringBitmap:mainfrom
emschwartz:shrink-to-fit

Conversation

@emschwartz

Copy link
Copy Markdown
Contributor

Closes #136.

RoaringBitmap::shrink_to_fit shrinks the container vector and every array and run store to its length. Bitmap stores are fixed-size boxes, so there is nothing to shrink there. RoaringTreemap::shrink_to_fit calls it on each bitmap and leaves empty partitions alone, since removing them is optimize's job.

The use case is the one @fjmaco measured on the issue: intersection and difference results keep the capacity of their inputs, so a 55,887-value intersection of two ~970K-value bitmaps holds 1,889 KiB of heap where a clone of it holds 119 KiB. Today that memory only comes back by rebuilding the bitmap, for example with *bitmap = bitmap.clone(). CRoaring has roaring_bitmap_shrink_to_fit for the same reason. This version returns () rather than CRoaring's count of freed bytes, to behave like Vec::shrink_to_fit, which is what a Rust caller will expect from the name. fjmaco offered on the issue to write this too. Happy to close this if fjmaco's version is underway.

Values and store kinds are unchanged. The one visible side effect is that statistics().n_bytes_array_containers goes down, since it is computed from capacity.

Tested with new unit tests (an intersection that leaves slack in all three places, an emptied bitmap, a two-partition treemap) and doctests. cargo mutants catches all 5 mutants of the new functions.

Authored with Claude (claude-opus-5-5).

Intersections and differences size their result arrays for the inputs, so
a small result can keep most of its input's capacity. shrink_to_fit frees
that memory by shrinking the container vector and every array and run
store, like CRoaring's roaring_bitmap_shrink_to_fit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

Feature: shrink_to_fit

1 participant