Repository navigation
Speed up bitset intersections, 64-bit shrink_to_fit, run counting and scalar array merges - #904
Merged
Merged
Conversation
… scalar array merges - Extract bitset intersections into arrays without a branch per word (AVX-512 VBMI2 and portable versions), using the known cardinality. - Make art_shrink_to_fit linear instead of quadratic. - Count runs in array containers with a loop compilers vectorize. - Hybrid branchless scalar merges (intersect, union, xor, difference) with block shortcuts for clustered data. - Add tests against reference implementations.
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.
This PR speeds up several hot paths found while benchmarking set operations on 64-bit bitmaps. Builds without SIMD (for example with
-DROARING_DISABLE_X64, or on targets without SIMD kernels) use the scalar paths, so both the scalar and SIMD paths are covered.Bitset ∩ bitset → array extraction (
bitset_util.c,mixed_intersection.c). When two bitset containers intersect to 4096 values or fewer, we extract the common bits into an array. The existing loop mispredicts about once per value on sparse random results. Sincebitset_container_and_justcardalready gives us the cardinality, there are two new extractors:cardvalues are written (empty-mask stores past the last value can trigger slow microcode assists);Per container, on random data: about 5.9 → 2.7 µs scalar and 1.5 µs with AVX-512 at 1% density; about 9.2 → 4.4 µs scalar at 4%. On clustered bits the scalar version is up to 1.45× slower in the worst case tested. At 10% density,
roaring64_bitmap_andgoes from 10.4 ms to 3.9 ms.art_shrink_to_fitis now linear. It moved nodes one at a time and walked the sorted free list for each, which is quadratic. With 64,000 containers and half of them removed,roaring64_bitmap_shrink_to_fittook 2.4 s; it now takes 1.3 ms. The new version ranks the live nodes of each type, rewrites the child references, and slides the nodes down. It also frees an emptied node array instead of callingrealloc(p, 0).array_container_number_of_runshas no branch per value, so compilers vectorize it. In a copy, in-placeAND,run_optimizesequence on 64-bit bitmaps at 20% density, therun_optimizestep drops from 2.3 ms to 0.4 ms.Scalar array merges (
intersect_uint16,intersect_uint16_cardinality,union_uint16,xor_uint16,difference_uint16). While four values remain on each side, they first look for a block of four to handle at once: all below the other side, or equal to the other side's next four. Otherwise they take four branchless steps.ANDat 1% density (copy, then in-placeAND) goes from 8.3 to 5.6 ms.out == Afor intersection (andout == Bis handled by swapping the inputs), union into a buffer that startssize_2values beforeset_1, and difference intoa1.Measurements: Intel Xeon Gold 6548N (Emerald Rapids) and Apple Silicon.
Tests: new unit tests compare the extractors and the merges with reference implementations, covering random and clustered data and every in-place layout. There are also tests for run counting and for
shrink_to_fiton many containers with all inner-node types. The full suite passes under ASan (arm64) and on x64 with AVX-512.