Skip to content

Speed up bitset intersections, 64-bit shrink_to_fit, run counting and scalar array merges - #904

Merged
lemire merged 3 commits into
masterfrom
branchless-intersection-extract
Oct 15, 2026
Merged

lemire merged 3 commits into
masterfrom
branchless-intersection-extract

Conversation

@lemire

@lemire lemire commented Oct 14, 2026

Copy link
Copy Markdown
Member

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.

  1. 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. Since bitset_container_and_justcard already gives us the cardinality, there are two new extractors:

    • an AVX-512 VBMI2 version with no branch per word, which stops once all card values are written (empty-mask stores past the last value can trigger slow microcode assists);
    • a portable version that writes K values per word unconditionally (K picked from the average density) and skips groups of four empty words.

    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_and goes from 10.4 ms to 3.9 ms.

  2. art_shrink_to_fit is 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_fit took 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 calling realloc(p, 0).

  3. array_container_number_of_runs has no branch per value, so compilers vectorize it. In a copy, in-place AND, run_optimize sequence on 64-bit bitmaps at 20% density, the run_optimize step drops from 2.3 ms to 0.4 ms.

  4. 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.

    • Random inputs (512 distinct pairs): 1.4–1.6× faster on x64 and Apple Silicon. For example, a 646 × 646 intersection goes from 5.1 to 3.4 µs on x64.
    • Clustered inputs: generally faster than before, often 2–4×. One regression: intersecting a small set inside a dense one is 1.26× slower on x64.
    • Without SIMD, a 64-bit AND at 1% density (copy, then in-place AND) goes from 8.3 to 5.6 ms.
    • The in-place uses are unchanged: out == A for intersection (and out == B is handled by swapping the inputs), union into a buffer that starts size_2 values before set_1, and difference into a1.

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_fit on many containers with all inner-node types. The full suite passes under ASan (arm64) and on x64 with AVX-512.

… 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.
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.

1 participant