Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arraycontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ func (ac *arrayContainer) toEfficientContainer() container {
sizeAsBitmapContainer := bitmapContainerSizeInBytes()
card := ac.getCardinality()
sizeAsArrayContainer := arrayContainerSizeInBytes(card)
if sizeAsRunContainer < minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) {
if numRuns <= maxRunsInRunContainer && sizeAsRunContainer < minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) {
return newRunContainer16FromArrayRuns(ac, numRuns)
}
if card <= arrayDefaultMaxSize {
Expand Down
2 changes: 1 addition & 1 deletion bitmapcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ func (bc *bitmapContainer) toEfficientContainer() container {
numRuns := bc.numberOfRunsUpTo(minSize / perIntervalRc16Size)
sizeAsRunContainer := runContainer16SerializedSizeInBytes(numRuns)

if sizeAsRunContainer < minSize {
if numRuns <= maxRunsInRunContainer && sizeAsRunContainer < minSize {
return newRunContainer16FromBitmapContainerRuns(bc, numRuns)
}
if card <= arrayDefaultMaxSize {
Expand Down
5 changes: 3 additions & 2 deletions bitmapcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func TestBitmapContainerIAndNot(t *testing.T) {
for i := 0; i < arrayDefaultMaxSize-3; i++ {
rc = rc.iaddRange(i*3, i*3+1)
}
rc.iaddRange(math.MaxUint16-3, math.MaxUint16+1)
rc = rc.iaddRange(math.MaxUint16-3, math.MaxUint16+1)

bc = bc.iandNot(rc)

Expand Down Expand Up @@ -783,7 +783,8 @@ func TestBitmapContainerToEfficientContainerChoice(t *testing.T) {
card := bc.getCardinality()
want := bitmapContype
switch {
case runContainer16SerializedSizeInBytes(numRuns) < min(bitmapContainerSizeInBytes(), arrayContainerSizeInBytes(card)):
case numRuns <= maxRunsInRunContainer &&
runContainer16SerializedSizeInBytes(numRuns) < min(bitmapContainerSizeInBytes(), arrayContainerSizeInBytes(card)):
want = run16Contype
case card <= arrayDefaultMaxSize:
want = arrayContype
Expand Down
11 changes: 7 additions & 4 deletions roaring64/roaring64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2142,16 +2142,19 @@ func TestRoaringArray64SortOrder(t *testing.T) {
})
}

func TestRunOptimizeRecoversSpaceAfterRemoveRange(t *testing.T) {
func TestRemoveRangeDoesNotGrowRunsPastBitmapSize(t *testing.T) {
bm := New()
bm.AddRange(0, 65536)
for i := uint64(0); i < 65536; i += 2 {
bm.RemoveRange(i, i+1) // each remaining value becomes a run of length one
bm.RemoveRange(i, i+1) // each remaining value would be a run of length one
}
// A run container with 32768 runs would take about 128 KiB; once it
// passes 2047 runs it must become a bitmap container instead.
assert.False(t, bm.HasRunCompression())
assert.Less(t, bm.GetSerializedSizeInBytes(), uint64(8192+64))
assert.Equal(t, uint64(32768), bm.GetCardinality())
expected := bm.Clone()
before := bm.GetSerializedSizeInBytes()
bm.RunOptimize()
assert.Less(t, bm.GetSerializedSizeInBytes(), before/10)
assert.False(t, bm.HasRunCompression())
assert.True(t, expected.Equals(bm))
}
17 changes: 10 additions & 7 deletions roaring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func hashTest(t *testing.T, N uint64) {
4: {0: 3431046584319368119, 1: 8614077757284407143},
8: {0: 10577784705589702583, 1: 11227064421971279799},
16: {0: 13582283525559942071, 1: 8280698677262929847},
32: {0: 15930731229079543980, 1: 14540819050448025772},
32: {0: 15930731229079543980, 1: 8379075619665720492},
64: {0: 15074197189812092501, 1: 8183429771446961749},
128: {0: 16353918313334514501, 1: 8211290591646809413},
256: {0: 17541269277318240901, 1: 15870350090812465285},
Expand All @@ -474,7 +474,7 @@ func hashTest(t *testing.T, N uint64) {
4: {0: 4410347288001337270, 1: 8610110719330578755},
8: {0: 8603373155648831414, 1: 2315836927982075848},
16: {0: 16944482119189024652, 1: 4822482652523786870},
32: {0: 6928244183881028666, 1: 10404343182114103400},
32: {0: 6928244183881028666, 1: 17964257898188937832},
64: {0: 3024916132362050707, 1: 18344738716466403161},
128: {0: 3834944388080394183, 1: 11965577797857582925},
256: {0: 12578054101381780383, 1: 6432236967053365413},
Expand All @@ -493,7 +493,7 @@ func hashTest(t *testing.T, N uint64) {
4: {0: 2746848181595101094, 1: 8606425156353572383},
8: {0: 16463913002427077937, 1: 4531009983748580157},
16: {0: 5620696034248496124, 1: 1681598425519654588},
32: {0: 14741226801656710698, 1: 15129443590117138858},
32: {0: 14741226801656710698, 1: 12294437372374694826},
64: {0: 6054465815016208075, 1: 17167720527236979867},
128: {0: 16740558302224955231, 1: 6094338199568812847},
256: {0: 16186283226139238340, 1: 9873750830371717112},
Expand Down Expand Up @@ -4294,16 +4294,19 @@ func TestValidateAcceptsRunContainersThatAreNotTheSmallestEncoding(t *testing.T)
assert.True(t, expected.Equals(deserialized))
}

func TestRunOptimizeRecoversSpaceAfterRemoveRange(t *testing.T) {
func TestRemoveRangeDoesNotGrowRunsPastBitmapSize(t *testing.T) {
bm := New()
bm.AddRange(0, 65536)
for i := uint64(0); i < 65536; i += 2 {
bm.RemoveRange(i, i+1) // each remaining value becomes a run of length one
bm.RemoveRange(i, i+1) // each remaining value would be a run of length one
}
// A run container with 32768 runs would take about 128 KiB; once it
// passes 2047 runs it must become a bitmap container instead.
assert.False(t, bm.HasRunCompression())
assert.Less(t, bm.GetSerializedSizeInBytes(), uint64(8192+64))
assert.Equal(t, uint64(32768), bm.GetCardinality())
expected := bm.Clone()
before := bm.GetSerializedSizeInBytes()
bm.RunOptimize()
assert.Less(t, bm.GetSerializedSizeInBytes(), before/10)
assert.False(t, bm.HasRunCompression())
assert.True(t, expected.Equals(bm))
require.NoError(t, bm.Validate())
Expand Down
31 changes: 25 additions & 6 deletions runcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ var (
MaxIntervalsSum = 2048
)

// maxRunsInRunContainer is the most runs a run container may hold. With
// 2048 runs or more, a serialized run container (4 bytes per run plus 2
// bytes) is larger than the 8192 bytes of a bitmap container's words, so
// operations convert it to an array or a bitmap container instead. The other
// Roaring implementations never produce such run containers either.
const maxRunsInRunContainer = 2047

// limitRuns returns rc if it has at most maxRunsInRunContainer runs, and
// otherwise an equivalent array or bitmap container.
func (rc *runContainer16) limitRuns() container {
if len(rc.iv) <= maxRunsInRunContainer {
return rc
}
if rc.getCardinality() <= arrayDefaultMaxSize {
return rc.toArrayContainer()
}
return newBitmapContainerFromRun(rc)
}

func newInterval16Range(start, last uint16) interval16 {
if last < start {
panic(fmt.Sprintf("last (%d) cannot be smaller than start (%d)", last, start))
Expand Down Expand Up @@ -2117,7 +2136,7 @@ func (rc *runContainer16) iaddRange(firstOfRange, endx int) container {
},
})
*rc = *rc.union(addme)
return rc
return rc.limitRuns()
}

// remove the values in the range [firstOfRange,endx)
Expand All @@ -2132,7 +2151,7 @@ func (rc *runContainer16) iremoveRange(firstOfRange, endx int) container {
}
x := newInterval16Range(uint16(firstOfRange), uint16(endx-1))
rc.isubtract(x)
return rc
return rc.limitRuns()
}

// not flip the values in the range [firstOfRange,endx)
Expand All @@ -2141,7 +2160,7 @@ func (rc *runContainer16) not(firstOfRange, endx int) container {
panic(fmt.Sprintf("invalid %v = endx > firstOfRange = %v", endx, firstOfRange))
}

return rc.Not(firstOfRange, endx)
return rc.Not(firstOfRange, endx).limitRuns()
}

// Not flips the values in the range [firstOfRange,endx).
Expand Down Expand Up @@ -2253,7 +2272,7 @@ func (rc *runContainer16) or(a container) container {
}
switch c := a.(type) {
case *runContainer16:
return rc.union(c)
return rc.union(c).limitRuns()
case *arrayContainer:
return rc.orArray(c)
case *bitmapContainer:
Expand Down Expand Up @@ -2788,7 +2807,7 @@ func (rc *runContainer16) toEfficientContainer() container {
sizeAsBitmapContainer := bitmapContainerSizeInBytes()
card := rc.getCardinality()
sizeAsArrayContainer := arrayContainerSizeInBytes(card)
if sizeAsRunContainer < minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) {
if len(rc.iv) <= maxRunsInRunContainer && sizeAsRunContainer < minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) {
return rc
}
if card <= arrayDefaultMaxSize {
Expand All @@ -2802,7 +2821,7 @@ func (rc *runContainer16) toEfficientContainerFromCardinality(card int) containe
sizeAsRunContainer := rc.getSizeInBytes()
sizeAsBitmapContainer := bitmapContainerSizeInBytes()
sizeAsArrayContainer := arrayContainerSizeInBytes(card)
if sizeAsRunContainer < minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) {
if len(rc.iv) <= maxRunsInRunContainer && sizeAsRunContainer < minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) {
return rc
}
if card <= arrayDefaultMaxSize {
Expand Down
Loading
Loading