Repository navigation
Use a single-pass merge in FastOr and roaring64 Or/Xor - #608
Merged
Merged
Conversation
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.
This was referenced Dec 3, 2026
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
Follow-up to #599, which noted that the in-place
lazyOR(behindFastOr) and the roaring64Orstill insert each source-only key withinsertNewKeyValueAt. Each insert shifts the rest of the receiver, so many interleaved source-only keys make the merge quadratic. roaring64Xorhad the same problem, plusremoveAtIndexfor 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
OrandXoralready do withmergeBulk.Changes
roaringArray.mergeBulktakes amergeOp(mergeOr,mergeXor,mergeLazyOr) instead ofxor bool.lazyORhands off tomergeBulk(..., mergeLazyOr)on the first source-only key. Its run-to-bitmap promotion beforelazyIOR(implement lazyOR and lazyIOR for runContainer16 #81) moves togetLazyUnionedWritableContainer, which both paths use.roaringArray64.mergeBulkmirrors the 32-bit one. roaring64OrandXoruse it for source-only keys and, inXor, for aligned containers that cancel to empty.Bug fix: roaring64 Xor shared source containers
On master, roaring64
Xorinserted the source's own container for a key the receiver lacked, without cloning it. Mutating the receiver afterwards changed the source:Interior source-only containers are now cloned, as in
Or.TestOrXorDoNotShareSourceContainersfails on master and passes here.Testing
TestFastOrBulkMergeFixtures: the existingOrbulk-merge fixtures, run throughFastOr.TestFastOrInterleavedParts: 64 bitmaps mixing array, bitmap and run containers, with interleaved keys. ChecksFastOragainstOrand checks that mutating the result leaves the inputs unchanged.TestOrXorBulkMerge:OrandXoragainst a reference result, with interleaved keys and aligned pairs that cancel or survive.TestOrXorDoNotShareSourceContainers: copy-on-write off and on, for interior and trailing keys.go test ./...andgo test -tags appengine ./...pass.gofmt -s -l .andgo tool unconvert ./...report nothing.Performance
Apple M4 Max,
benchstat, n=6.The existing
BitmapOrBulkMerge,BitmapXorBulkMergeandOrAscendingPartsrows are unchanged; re-running them interleaved with master gives p > 0.2.Trade-offs:
Xorat 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.FastOrwith many interleaved parts allocates more bytes in total (7.7 MiB to 40.5 MiB with 64 parts), because eachlazyORcall 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 afterFrozenViewthe key slice can point into the caller's buffer. It would be safe forFastOr, whose receiver it always builds itself, and could be a follow-up.