Decode whole words in bulk in bitmapContainer's many-iterator - #560
Merged
Conversation
nextMany walked set bits one at a time. When the current word is exhausted
it now decodes whole words with the AVX-512 kernel for as long as the
caller's buffer has room for the 64 values a single word could produce,
falling back to the word-at-a-time loop for the partial word. bitset stays
zero across the bulk step, preserving the invariant that word `base` has
been fully consumed.
The word count per step is room/64, which cannot overflow the buffer
whatever the data looks like.
Xeon Gold 6548N, ns per value, 4096-entry buffer:
cardinality 4096 1.240 -> 0.418 (2.97x)
cardinality 16384 1.348 -> 0.130 (10.4x)
cardinality 32768 1.229 -> 0.097 (12.7x)
cardinality 65536 1.021 -> 0.074 (13.8x)
BenchmarkRealDataNextMany over the whole corpus:
census-income 10.19 ms -> 3.41 ms (2.99x)
weather_sept_85 20.22 ms -> 7.42 ms (2.72x)
A 64-entry buffer is the worst case, one word per bulk step; it is still
slightly faster (1.21x), not a regression.
nextMany64 is left alone: []uint64 output needs a byte-to-quadword widen,
a different kernel with a different trade-off.
Claude-Session: https://claude.ai/code/session_0123ePBefqPjrCvhxdwWFakj
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.
Description
NextManyon a dense bitmap walks set bits one at a time. This decodes whole words in bulk with the AVX-512 kernel added in #557.Stacked on #557 - it reuses
fillLeastSignificant16bitsVectorand theuseVectorFillgate from that PR, so the base branch isvbmi2-toarray, notmaster. Review #557 first; the diff here is justbitmapcontainer.goplus a test.Type of Change
Changes Made
bitmapContainerManyIterator.nextManykeeps its word-at-a-time loop for the partially consumed word, but when the current word is exhausted it now decodes whole words in bulk for as long as the caller's buffer has room for the 64 values a single word could produce.bitsetstays zero across the bulk step, preserving the iterator's invariant that wordbasehas been fully consumed, so the scalar loop picks up correctly afterwards and the saved state is unchanged in shape.The word count per bulk step is
room / 64, which cannot overflow the buffer whatever the data looks like. With a 4096-entry buffer that is 64 words per step, so a full container takes a handful of steps.nextMany64is deliberately not changed. Its output is[]uint64, which would need a byte-to-quadword widen - eight 64-byte stores per input word instead of two - and that is a different kernel with a different trade-off. Worth measuring separately rather than assuming it carries over.Performance Impact
Xeon Gold 6548N (Emerald Rapids), pinned with
taskset, Go 1.26.3.Iterating a whole bitmap container, ns per value:
A 64-entry buffer is the worst case: only one word per bulk step, so almost everything goes through the scalar loop. It is still slightly faster, not a regression.
BenchmarkRealDataNextManyover the whole corpus:census-incomeweather_sept_85Testing
go test ./...,roaring64, the smat corpus and smat-hit checks,go tool unconvert,gofmt,go vet, and cross-builds for 386/arm/arm64 and-tags appengine, on AVX-512 hardware so the bulk path executes.TestBitmapContainerNextManyBufferSizesiterates containers of cardinality 1, 63, 64, 65, 4096, 30000, 65535 and 65536 through buffers of 1, 2, 63, 64, 65, 100, 127, 128, 129, 1000, 4096 and 65536 entries, comparing the concatenated output against the expected values and checking that no call ever returns more than the buffer holds. The sizes that are not multiples of 64 are the point: they force the bulk and scalar paths to interleave mid-container.Breaking Changes
None.
NextMany's contract, ordering and buffer handling are unchanged.https://claude.ai/code/session_0123ePBefqPjrCvhxdwWFakj