Repository navigation
Skip the len() pass over the last bitmap in RoaringTreemap::select - #374
Merged
Merged
Conversation
Member
RoaringTreemap::select calls len() on each bitmap, which walks all of its containers, and then select() on the bitmap that holds the target, which walks them again. The last bitmap does not need the len() pass, since RoaringBitmap::select already returns None when n is past its end. A treemap whose values all fit in a u32 has a single bitmap, so every select on it walked the whole bitmap before selecting. On a Ryzen 9 7900X such selects become about 1.4-3x faster at the middle and the end of the set and up to ~100x faster near the start. A select that lands in the last of several bitmaps also skips that bitmap's len() pass. The loop over the other bitmaps is unchanged. Add a property test that checks select against the iterator.
Kerollmops
force-pushed
the
treemap-select-no-recount
branch
from
October 27, 2026 13:28
5105419 to
0534e6e
Compare
Kerollmops
approved these changes
Oct 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RoaringTreemap::selectcallslen()on each bitmap, which walks all of its containers, and thenselect()on the bitmap that holds the target, which walks them again. The last bitmap doesn't need thelen()pass:RoaringBitmap::selectalready returnsNonewhennis past its end.This mostly helps treemaps whose values all fit in a
u32. They have a single bitmap, so everyselectwalked the whole bitmap before selecting. Aselectthat lands in the last of several bitmaps also skips that bitmap'slen()pass. The loop over the other bitmaps is unchanged.Speedup of
select(main time / branch time). Medians over 6 interleaved runs on a Ryzen 9 7900X pinned to two cores. Each side was built in 4 code layouts, and the brackets give the range over this branch's 4 layouts:-C target-cpu=native"Main" is with
-C target-cpu=native. With many small bitmaps nothing changes algorithmically. Across 8 shapes with 1k–100k bitmaps of 1–64 containers each, at the 1%/50%/last positions, the results range from 0.97x to 1.18x. The slowest rows are within the ~3% spread I measured between differently laid-out builds of unchangedmain.I first tried a single pass over every bitmap, selecting while subtracting container lengths with no
len()at all. It wins on few-bitmap treemaps, but on treemaps with 10k-100k small bitmaps it was slower in most builds I tried (up to about 1.6x) and moved a lot with code layout:len()'s plain sum seems to be cheaper than an early-exit loop when a whole bitmap is skipped. Keepinglen()for all but the last bitmap avoids that. (An O(log n) select across bitmaps would need cached cardinalities; see the constant-timelenwork in #45, #105 and #182. This PR doesn't go there.)The new property test checks
selectagainst the iterator for the first and last value of every bitmap plus random positions, including treemaps built withfrom_bitmapsthat contain empty bitmaps.Checked locally, with the
RUSTFLAGSfromtest.yml:cargo fmt --all -- --checktest.ymlon stable (1.99.0), beta, nightly and 1.90.0, plus nightly--all-featurescargo test -p roaring --features serdeand--no-default-featureson stable, beta, nightly and 1.90.0cargo +nightly test -p roaring --features simdcargo test -p benchmarks --bencheson stable, beta and nightlyselect/select_emptytests and theRoaringTreemap::selectdoctest under miriThis adds a test at the end of the same test module as #366, so whichever lands second needs a trivial rebase.
Happy to share the benchmark harness or turn it into a criterion bench if that's useful.