summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/RoaringBitmap/roaring/arraycontainer.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/RoaringBitmap/roaring/arraycontainer.go')
-rw-r--r--vendor/github.com/RoaringBitmap/roaring/arraycontainer.go38
1 files changed, 17 insertions, 21 deletions
diff --git a/vendor/github.com/RoaringBitmap/roaring/arraycontainer.go b/vendor/github.com/RoaringBitmap/roaring/arraycontainer.go
index 12fe6cf530..f8bb29b96f 100644
--- a/vendor/github.com/RoaringBitmap/roaring/arraycontainer.go
+++ b/vendor/github.com/RoaringBitmap/roaring/arraycontainer.go
@@ -359,28 +359,17 @@ func (ac *arrayContainer) iorArray(value2 *arrayContainer) container {
len1 := value1.getCardinality()
len2 := value2.getCardinality()
maxPossibleCardinality := len1 + len2
- if maxPossibleCardinality > arrayDefaultMaxSize { // it could be a bitmap!
- bc := newBitmapContainer()
- for k := 0; k < len(value2.content); k++ {
- v := value2.content[k]
- i := uint(v) >> 6
- mask := uint64(1) << (v % 64)
- bc.bitmap[i] |= mask
- }
- for k := 0; k < len(ac.content); k++ {
- v := ac.content[k]
- i := uint(v) >> 6
- mask := uint64(1) << (v % 64)
- bc.bitmap[i] |= mask
- }
- bc.cardinality = int(popcntSlice(bc.bitmap))
- if bc.cardinality <= arrayDefaultMaxSize {
- return bc.toArrayContainer()
- }
- return bc
- }
if maxPossibleCardinality > cap(value1.content) {
- newcontent := make([]uint16, 0, maxPossibleCardinality)
+ // doubling the capacity reduces new slice allocations in the case of
+ // repeated calls to iorArray().
+ newSize := 2 * maxPossibleCardinality
+ // the second check is to handle overly large array containers
+ // and should not occur in normal usage,
+ // as all array containers should be at most arrayDefaultMaxSize
+ if newSize > 2*arrayDefaultMaxSize && maxPossibleCardinality <= 2*arrayDefaultMaxSize {
+ newSize = 2 * arrayDefaultMaxSize
+ }
+ newcontent := make([]uint16, 0, newSize)
copy(newcontent[len2:maxPossibleCardinality], ac.content[0:len1])
ac.content = newcontent
} else {
@@ -388,6 +377,13 @@ func (ac *arrayContainer) iorArray(value2 *arrayContainer) container {
}
nl := union2by2(value1.content[len2:maxPossibleCardinality], value2.content, ac.content)
ac.content = ac.content[:nl] // reslice to match actual used capacity
+
+ if nl > arrayDefaultMaxSize {
+ // Only converting to a bitmap when arrayDefaultMaxSize
+ // is actually exceeded minimizes conversions in the case of repeated
+ // calls to iorArray().
+ return ac.toBitmapContainer()
+ }
return ac
}