Skip to content

Commit

Permalink
clone to buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
aliszka committed Dec 5, 2024
1 parent 7157ab6 commit 6aaaca0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
19 changes: 19 additions & 0 deletions benchmark_opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,32 @@ import (
"testing"
)

var prefilled *Bitmap
var prefilledBuf []byte

// func init() {
// initPrefilled()
// }

func initPrefilled() {
prefilled = Prefill(200_000_000)
prefilledBuf = make([]byte, prefilled.LenBytes())
}

// go test -v -bench BenchmarkPrefillNative -benchmem -run ^$ github.com/weaviate/sroar -cpuprofile cpu.prof
func BenchmarkPrefillNative(b *testing.B) {
for i := 0; i < b.N; i++ {
Prefill(200_000_000)
}
}

// go test -v -bench BenchmarkPrefillCloneToBuf -benchmem -run ^$ github.com/weaviate/sroar -cpuprofile cpu.prof
func BenchmarkPrefillCloneToBuf(b *testing.B) {
for i := 0; i < b.N; i++ {
prefilled.CloneToBuf(prefilledBuf)
}
}

// go test -v -bench BenchmarkPrefillFromSortedList -benchmem -run ^$ github.com/weaviate/sroar -cpuprofile cpu.prof
func BenchmarkPrefillFromSortedList(b *testing.B) {
prefillBufferSize := 65_536
Expand Down
20 changes: 20 additions & 0 deletions bitmap_opt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sroar

import (
"fmt"
"math"
"sync"
)
Expand Down Expand Up @@ -746,3 +747,22 @@ func (ra *Bitmap) CapBytes() int {
}
return cap(ra.data) * 2
}

func (ra *Bitmap) CloneToBuf(buf []byte) *Bitmap {
if len(buf)%2 != 0 {
panic(fmt.Sprintf("Buffer size should be even, given %d", len(buf)))
}

a := ra
if ra == nil {
a = NewBitmap()
}

if alen := a.LenBytes(); alen > len(buf) {
panic(fmt.Sprintf("Buffer too small, given %d, required %d", len(buf), alen))
}

abuf := toByteSlice(a.data)
copy(buf, abuf)
return FromBuffer(buf)
}
44 changes: 44 additions & 0 deletions bitmap_opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,50 @@ func TestCapBytes(t *testing.T) {
})
}

func TestCloneToBuf(t *testing.T) {
t.Run("non-nil bitmap", func(t *testing.T) {
bmEmpty := NewBitmap()

bm1 := NewBitmap()
bm1.Set(1)

bm2 := NewBitmap()
bm2.Set(1)
bm2.Set(1 + uint64(maxCardinality))

bm3 := NewBitmap()
bm3.Set(1)
bm3.Set(1 + uint64(maxCardinality))
bm3.Set(1 + uint64(maxCardinality)*2)

for name, bm := range map[string]*Bitmap{
"empty": bmEmpty,
"bm1": bm1,
"bm2": bm2,
"bm3": bm3,
} {
t.Run(name, func(t *testing.T) {
buf := make([]byte, bm.LenBytes())
cloned := bm.CloneToBuf(buf)

require.Equal(t, bm.GetCardinality(), cloned.GetCardinality())
require.Equal(t, bm.LenBytes(), cloned.LenBytes())
})
}
})

t.Run("nil bitmap", func(t *testing.T) {
var bmNil *Bitmap
bmEmpty := NewBitmap()

buf := make([]byte, bmEmpty.LenBytes())
cloned := bmNil.CloneToBuf(buf)

require.Equal(t, bmEmpty.GetCardinality(), cloned.GetCardinality())
require.Equal(t, bmEmpty.LenBytes(), cloned.LenBytes())
})
}

func TestMergeToSuperset(t *testing.T) {
run := func(t *testing.T, bufs [][]uint16) {
containerThreshold := uint64(math.MaxUint16 + 1)
Expand Down

0 comments on commit 6aaaca0

Please sign in to comment.