Skip to content

Use a single-pass merge in FastOr and roaring64 Or/Xor - #608

Merged
lemire merged 1 commit into
masterfrom
or-bulk-merge-lazy
Dec 4, 2026
Merged

lemire merged 1 commit into
masterfrom
or-bulk-merge-lazy

Conversation

@lemire

@lemire lemire commented Dec 3, 2026

Copy link
Copy Markdown
Member

Description

Follow-up to #599, which noted that the in-place lazyOR (behind FastOr) and the roaring64 Or still insert each source-only key with insertNewKeyValueAt. Each insert shifts the rest of the receiver, so many interleaved source-only keys make the merge quadratic. roaring64 Xor had the same problem, plus removeAtIndex for containers that cancel to empty.

These now finish the merge in one pass into fresh slices once the receiver's structure must change, as the 32-bit Or and Xor already do with mergeBulk.

Changes

  • roaringArray.mergeBulk takes a mergeOp (mergeOr, mergeXor, mergeLazyOr) instead of xor bool.
  • lazyOR hands off to mergeBulk(..., mergeLazyOr) on the first source-only key. Its run-to-bitmap promotion before lazyIOR (implement lazyOR and lazyIOR for runContainer16 #81) moves to getLazyUnionedWritableContainer, which both paths use.
  • roaringArray64.mergeBulk mirrors the 32-bit one. roaring64 Or and Xor use it for source-only keys and, in Xor, for aligned containers that cancel to empty.

Bug fix: roaring64 Xor shared source containers

On master, roaring64 Xor inserted the source's own container for a key the receiver lacked, without cloning it. Mutating the receiver afterwards changed the source:

a := roaring64.BitmapOf(0, 2<<32)
b := roaring64.BitmapOf(1 << 32)
a.Xor(b)
a.Add(1<<32 | 5) // b now contains 1<<32|5 as well

Interior source-only containers are now cloned, as in Or. TestOrXorDoNotShareSourceContainers fails on master and passes here.

Testing

  • TestFastOrBulkMergeFixtures: the existing Or bulk-merge fixtures, run through FastOr.
  • TestFastOrInterleavedParts: 64 bitmaps mixing array, bitmap and run containers, with interleaved keys. Checks FastOr against Or and checks that mutating the result leaves the inputs unchanged.
  • roaring64 TestOrXorBulkMerge: Or and Xor against a reference result, with interleaved keys and aligned pairs that cancel or survive.
  • roaring64 TestOrXorDoNotShareSourceContainers: copy-on-write off and on, for interior and trailing keys.

go test ./... and go test -tags appengine ./... pass. gofmt -s -l . and go tool unconvert ./... report nothing.

Performance

Apple M4 Max, benchstat, n=6.

                                    master       this PR
FastOrInterleaved/parts=8           249.3ms      3.314ms   -98.67%
FastOrInterleaved/parts=64          287.6ms      15.31ms   -94.68%
roaring64 OrXorInterleaved/Or/16384  26.9ms      3.147ms   -88.29%
roaring64 OrXorInterleaved/Xor/16384 23.7ms      3.103ms   -86.94%
roaring64 OrXorInterleaved/Or/1024  277.3µs      192.3µs   -30.66%
roaring64 OrXorInterleaved/Xor/1024 172.9µs      191.9µs   +10.96%

The existing BitmapOrBulkMerge, BitmapXorBulkMerge and OrAscendingParts rows are unchanged; re-running them interleaved with master gives p > 0.2.

Trade-offs:

  • roaring64 Xor at 1,024 keys is about 11% slower and makes twice as many allocations, because it now clones source-only containers instead of sharing them. That is the cost of the bug fix.
  • FastOr with many interleaved parts allocates more bytes in total (7.7 MiB to 40.5 MiB with 64 parts), because each lazyOR call that takes the bulk path builds fresh slices instead of growing the receiver's own. This is short-lived garbage, not peak memory. A backward merge into the receiver's slices would avoid it. It is not safe in general, because after FrozenView the key slice can point into the caller's buffer. It would be safe for FastOr, whose receiver it always builds itself, and could be a follow-up.

FastOr's in-place lazyOR, roaring64 Or and roaring64 Xor inserted each
source-only key (and Xor removed each cancelled container) in place, which
shifts the rest of the receiver every time and is quadratic when many keys
are interleaved. Once such a change is needed they now finish the merge in
one pass into fresh slices, as the 32-bit Or and Xor already do with
mergeBulk.

mergeBulk takes a mergeOp instead of a xor flag so that lazyOR can reuse
it, and roaringArray64 gets its own mergeBulk.

This also fixes roaring64 Xor sharing the source's container for a key the
receiver lacked: mutating the receiver afterwards changed the source.
@lemire
lemire merged commit 0a4f16f into master Dec 4, 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