Skip to content

Commit

Permalink
Avoid unneeded slice allocations for addOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
Oppen committed Nov 30, 2021
1 parent c8a81be commit 0f4ae5d
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions arraycontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ func (ac *arrayContainer) containerType() contype {
return arrayContype
}

func (ac *arrayContainer) addOffset(x uint16) []container {
func (ac *arrayContainer) addOffset(x uint16) (container, container) {
low := &arrayContainer{}
high := &arrayContainer{}
for _, val := range ac.content {
Expand All @@ -1018,5 +1018,5 @@ func (ac *arrayContainer) addOffset(x uint16) []container {
low.content = append(low.content, lowbits(y))
}
}
return []container{low, high}
return low, high
}
10 changes: 5 additions & 5 deletions arraycontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ func TestArrayOffset(t *testing.T) {
v = v.iaddReturnMinimized(n)
expected[i] = int(n) + int(offtest)
}
w := v.addOffset(offtest)
w0card := w[0].getCardinality()
w1card := w[1].getCardinality()
l, h := v.addOffset(offtest)
w0card := l.getCardinality()
w1card := h.getCardinality()

assert.Equal(t, 3, w0card+w1card)

wout := make([]int, len(nums))
for i := 0; i < w0card; i++ {
wout[i] = w[0].selectInt(uint16(i))
wout[i] = l.selectInt(uint16(i))
}
for i := 0; i < w1card; i++ {
wout[i+w0card] = w[1].selectInt(uint16(i)) + 65536
wout[i+w0card] = h.selectInt(uint16(i)) + 65536
}
for i, x := range wout {
assert.Equal(t, expected[i], x)
Expand Down
4 changes: 2 additions & 2 deletions bitmapcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ func (bc *bitmapContainer) containerType() contype {
return bitmapContype
}

func (bc *bitmapContainer) addOffset(x uint16) []container {
func (bc *bitmapContainer) addOffset(x uint16) (container, container) {
low := newBitmapContainer()
high := newBitmapContainer()
b := uint32(x) >> 6
Expand All @@ -1150,5 +1150,5 @@ func (bc *bitmapContainer) addOffset(x uint16) []container {
}
low.computeCardinality()
high.computeCardinality()
return []container{low, high}
return low, high
}
10 changes: 5 additions & 5 deletions bitmapcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,18 @@ func TestBitmapOffset(t *testing.T) {
v.iadd(n)
expected[i] = int(n) + int(offtest)
}
w := v.addOffset(offtest)
w0card := w[0].getCardinality()
w1card := w[1].getCardinality()
l, h := v.addOffset(offtest)
w0card := l.getCardinality()
w1card := h.getCardinality()

assert.Equal(t, v.getCardinality(), w0card+w1card)

wout := make([]int, len(nums))
for i := 0; i < w0card; i++ {
wout[i] = w[0].selectInt(uint16(i))
wout[i] = l.selectInt(uint16(i))
}
for i := 0; i < w1card; i++ {
wout[i+w0card] = w[1].selectInt(uint16(i)) + 65536
wout[i+w0card] = h.selectInt(uint16(i)) + 65536
}

for i, x := range wout {
Expand Down
12 changes: 6 additions & 6 deletions roaring.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,9 @@ func AddOffset64(x *Bitmap, offset int64) (answer *Bitmap) {
key += containerOffset

c := x.highlowcontainer.getContainerAtIndex(pos)
offsetted := c.addOffset(inOffset)
lo, hi := c.addOffset(inOffset)

if !offsetted[0].isEmpty() && (key >= 0 && key <= MaxUint16) {
if !lo.isEmpty() && (key >= 0 && key <= MaxUint16) {
curSize := answer.highlowcontainer.size()
lastkey := int32(0)

Expand All @@ -557,15 +557,15 @@ func AddOffset64(x *Bitmap, offset int64) (answer *Bitmap) {

if curSize > 0 && lastkey == key {
prev := answer.highlowcontainer.getContainerAtIndex(curSize - 1)
orrseult := prev.ior(offsetted[0])
orrseult := prev.ior(lo)
answer.highlowcontainer.setContainerAtIndex(curSize-1, orrseult)
} else {
answer.highlowcontainer.appendContainer(uint16(key), offsetted[0], false)
answer.highlowcontainer.appendContainer(uint16(key), lo, false)
}
}

if !offsetted[1].isEmpty() && ((key+1) >= 0 && (key+1) <= MaxUint16) {
answer.highlowcontainer.appendContainer(uint16(key+1), offsetted[1], false)
if !hi.isEmpty() && ((key+1) >= 0 && (key+1) <= MaxUint16) {
answer.highlowcontainer.appendContainer(uint16(key+1), hi, false)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion roaringarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type container interface {
addOffset(uint16) []container
addOffset(uint16) (container, container)

clone() container
and(container) container
Expand Down
4 changes: 2 additions & 2 deletions runcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2582,7 +2582,7 @@ func (rc *runContainer16) serializedSizeInBytes() int {
return 2 + len(rc.iv)*4
}

func (rc *runContainer16) addOffset(x uint16) []container {
func (rc *runContainer16) addOffset(x uint16) (container, container) {
low := newRunContainer16()
high := newRunContainer16()

Expand All @@ -2600,5 +2600,5 @@ func (rc *runContainer16) addOffset(x uint16) []container {
high.iv = append(high.iv, interval16{uint16(val & 0xffff), iv.length})
}
}
return []container{low, high}
return low, high
}
10 changes: 5 additions & 5 deletions runcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func TestRleInterval16s(t *testing.T) {
func TestRunOffset(t *testing.T) {
v := newRunContainer16TakeOwnership([]interval16{newInterval16Range(34, 39)})
offtest := uint16(65500)
w := v.addOffset(offtest)
w0card := w[0].getCardinality()
w1card := w[1].getCardinality()
l, h := v.addOffset(offtest)
w0card := l.getCardinality()
w1card := h.getCardinality()

if w0card+w1card != 6 {
t.Errorf("Bogus cardinality.")
Expand All @@ -91,10 +91,10 @@ func TestRunOffset(t *testing.T) {
expected := []int{65534, 65535, 65536, 65537, 65538, 65539}
wout := make([]int, len(expected))
for i := 0; i < w0card; i++ {
wout[i] = w[0].selectInt(uint16(i))
wout[i] = l.selectInt(uint16(i))
}
for i := 0; i < w1card; i++ {
wout[i+w0card] = w[1].selectInt(uint16(i)) + 65536
wout[i+w0card] = h.selectInt(uint16(i)) + 65536
}

for i, x := range wout {
Expand Down

0 comments on commit 0f4ae5d

Please sign in to comment.