Skip to content

roaring64: fewer bitmap operations in BSI Add and Increment - #610

Merged
lemire merged 2 commits into
masterfrom
bsi64-add-fewer-ops
Dec 5, 2026
Merged

lemire merged 2 commits into
masterfrom
bsi64-add-fewer-ops

Conversation

@lemire

@lemire lemire commented Dec 4, 2026

Copy link
Copy Markdown
Member

Follow-up to #609.

Changes

  • The sum bit is computed in place in x, so x^y is no longer computed twice per plane.
  • carry&(x^y) is computed in the carry bitmap itself (it equals carry &^ (x^y^carry)), which saves an allocation per plane.
  • A plane is skipped when there is no carry and the other operand's plane is empty. The loop stops once only the empty sign plane of a non-negative operand is left. This helps when b is wider than other, and Increment now stops as soon as the carry dies out.
  • Adds BenchmarkBSIAdd: 100k columns, cloning b outside the timer.

Benchmark (Apple M4 Max, mean of 6 runs)

case before after B/op before → after allocs before → after
same-width-signed (30 bits) 529 µs 348 µs 2.57 MB → 1.55 MB 1761 → 966
same-width-unsigned (30 bits) 489 µs 339 µs 2.49 MB → 1.50 MB 1710 → 938
narrow-other (40-bit b, 8-bit other) 283 µs 185 µs 1.58 MB → 0.73 MB 1422 → 581
increment (30 bits) 262 µs 122 µs 0.98 MB → 0.41 MB 1009 → 385

Testing

go test ./roaring64 passes. I also ran a local randomized check (not included) against big.Int over 5000 rounds: mixed widths from 1 to 62 bits, mixed signs, fixed-width and auto-sized BSIs, adding a BSI to itself, and repeated Increment on subsets that include new columns. It passes on master and on this branch.

addPlanes computed x^y twice per plane and allocated a temporary for
carry&(x^y). Compute the sum bit in place in x, and reuse the carry
bitmap for carry&(x^y), which equals carry&^(x^y^carry).

Skip planes where there is no carry and the other operand's plane is
empty, and stop once only the empty sign plane of a non-negative
operand is left. Increment stops as soon as the carry dies out.

Add BenchmarkBSIAdd.
Xor can install a carry container into the value plane. AndNot on that
carry then cleared the sum bit for column IDs above 2^32.
@lemire
lemire merged commit 5fe5059 into master Dec 5, 2026
14 checks passed
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