Skip to content

Commit

Permalink
orbuf method/fn
Browse files Browse the repository at this point in the history
  • Loading branch information
aliszka committed Nov 4, 2024
1 parent 49772c9 commit cb81762
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 35 deletions.
86 changes: 57 additions & 29 deletions benchmark_or.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,122 @@ go test -v -bench Benchmark_Or_ -benchmem -run ^$ github.com/weaviate/sroar
elements: 7

Benchmark_Or_Old
Benchmark_Or_Old-10 124358 8744 ns/op 2240 B/op 8 allocs/op
Benchmark_Or_Old-10 137358 8619 ns/op 2240 B/op 8 allocs/op
Benchmark_Or_OldFn
Benchmark_Or_OldFn-10 64314 19737 ns/op 35520 B/op 178 allocs/op
Benchmark_Or_OldFn-10 63716 18401 ns/op 35520 B/op 178 allocs/op
Benchmark_Or_Alt
Benchmark_Or_Alt-10 193574 5896 ns/op 2240 B/op 8 allocs/op
Benchmark_Or_Alt-10 193772 5872 ns/op 2240 B/op 8 allocs/op
Benchmark_Or_AltFn
Benchmark_Or_AltFn-10 75320 16180 ns/op 49984 B/op 208 allocs/op
Benchmark_Or_AltFn-10 71002 16506 ns/op 49984 B/op 208 allocs/op
Benchmark_Or_Buf
Benchmark_Or_Buf-10 666516 1710 ns/op 2240 B/op 8 allocs/op
Benchmark_Or_BufFn
Benchmark_Or_BufFn-10 98116 11774 ns/op 49984 B/op 208 allocs/op




elements: 67

Benchmark_Or_Old
Benchmark_Or_Old-10 73114 16408 ns/op 7936 B/op 12 allocs/op
Benchmark_Or_Old-10 71934 15913 ns/op 7936 B/op 12 allocs/op
Benchmark_Or_OldFn
Benchmark_Or_OldFn-10 45784 25726 ns/op 60032 B/op 210 allocs/op
Benchmark_Or_OldFn-10 45294 26045 ns/op 60032 B/op 210 allocs/op
Benchmark_Or_Alt
Benchmark_Or_Alt-10 80880 14287 ns/op 4864 B/op 10 allocs/op
Benchmark_Or_Alt-10 81104 14267 ns/op 4864 B/op 10 allocs/op
Benchmark_Or_AltFn
Benchmark_Or_AltFn-10 44790 26830 ns/op 66112 B/op 209 allocs/op
Benchmark_Or_AltFn-10 44512 26713 ns/op 66112 B/op 209 allocs/op
Benchmark_Or_Buf
Benchmark_Or_Buf-10 152304 7368 ns/op 4864 B/op 10 allocs/op
Benchmark_Or_BufFn
Benchmark_Or_BufFn-10 59793 19780 ns/op 66112 B/op 209 allocs/op




elements: 567

Benchmark_Or_Old
Benchmark_Or_Old-10 16183 72031 ns/op 36928 B/op 12 allocs/op
Benchmark_Or_Old-10 16530 70862 ns/op 36928 B/op 12 allocs/op
Benchmark_Or_OldFn
Benchmark_Or_OldFn-10 9369 109007 ns/op 388032 B/op 248 allocs/op
Benchmark_Or_OldFn-10 11068 109576 ns/op 388032 B/op 248 allocs/op
Benchmark_Or_Alt
Benchmark_Or_Alt-10 16832 70811 ns/op 36928 B/op 12 allocs/op
Benchmark_Or_Alt-10 16742 71016 ns/op 36928 B/op 12 allocs/op
Benchmark_Or_AltFn
Benchmark_Or_AltFn-10 9037 117504 ns/op 566656 B/op 248 allocs/op
Benchmark_Or_AltFn-10 8902 117368 ns/op 566656 B/op 248 allocs/op
Benchmark_Or_Buf
Benchmark_Or_Buf-10 18942 63016 ns/op 36928 B/op 12 allocs/op
Benchmark_Or_BufFn
Benchmark_Or_BufFn-10 9580 109518 ns/op 566656 B/op 248 allocs/op




elements: 4567

Benchmark_Or_Old
Benchmark_Or_Old-10 8365 125783 ns/op 136512 B/op 10 allocs/op
Benchmark_Or_Old-10 10142 123281 ns/op 136512 B/op 10 allocs/op
Benchmark_Or_OldFn
Benchmark_Or_OldFn-10 2625 435354 ns/op 2429763 B/op 248 allocs/op
Benchmark_Or_OldFn-10 2654 435234 ns/op 2429761 B/op 248 allocs/op
Benchmark_Or_Alt
Benchmark_Or_Alt-10 9212 121773 ns/op 136512 B/op 10 allocs/op
Benchmark_Or_Alt-10 9439 121141 ns/op 136512 B/op 10 allocs/op
Benchmark_Or_AltFn
Benchmark_Or_AltFn-10 3561 312373 ns/op 2740417 B/op 248 allocs/op
Benchmark_Or_AltFn-10 3621 316071 ns/op 2740417 B/op 248 allocs/op
Benchmark_Or_Buf
Benchmark_Or_Buf-10 9802 114179 ns/op 136512 B/op 10 allocs/op
Benchmark_Or_BufFn
Benchmark_Or_BufFn-10 3571 321915 ns/op 2740417 B/op 248 allocs/op




elements: 34567

Benchmark_Or_Old
Benchmark_Or_Old-10 4281 278046 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_Old-10 4275 279616 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_OldFn
Benchmark_Or_OldFn-10 2444 470096 ns/op 2876097 B/op 248 allocs/op
Benchmark_Or_OldFn-10 2479 475768 ns/op 2876098 B/op 248 allocs/op
Benchmark_Or_Alt
Benchmark_Or_Alt-10 4057 277929 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_Alt-10 4068 284528 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_AltFn
Benchmark_Or_AltFn-10 2379 498199 ns/op 2876097 B/op 248 allocs/op
Benchmark_Or_AltFn-10 2395 477734 ns/op 2876098 B/op 248 allocs/op
Benchmark_Or_Buf
Benchmark_Or_Buf-10 4262 272442 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_BufFn
Benchmark_Or_BufFn-10 2414 470769 ns/op 2876100 B/op 248 allocs/op




elements: 234567

Benchmark_Or_Old
Benchmark_Or_Old-10 3514 331398 ns/op 109377 B/op 8 allocs/op
Benchmark_Or_Old-10 3523 345830 ns/op 109377 B/op 8 allocs/op
Benchmark_Or_OldFn
Benchmark_Or_OldFn-10 2322 511811 ns/op 2876098 B/op 248 allocs/op
Benchmark_Or_OldFn-10 2230 545129 ns/op 2876100 B/op 248 allocs/op
Benchmark_Or_Alt
Benchmark_Or_Alt-10 10000 114039 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_Alt-10 10000 114571 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_AltFn
Benchmark_Or_AltFn-10 3792 333178 ns/op 2876096 B/op 248 allocs/op

Benchmark_Or_AltFn-10 3700 337825 ns/op 2876097 B/op 248 allocs/op
Benchmark_Or_Buf
Benchmark_Or_Buf-10 10000 106827 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_BufFn
Benchmark_Or_BufFn-10 3768 320828 ns/op 2876096 B/op 248 allocs/op



elements: 1234567

cpu: Apple M1 Pro
Benchmark_Or_Old
Benchmark_Or_Old-10 14146 83943 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_Old-10 14138 84844 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_OldFn
Benchmark_Or_OldFn-10 3848 319742 ns/op 2876096 B/op 248 allocs/op
Benchmark_Or_OldFn-10 4010 320525 ns/op 2876096 B/op 248 allocs/op
Benchmark_Or_Alt
Benchmark_Or_Alt-10 31942 37684 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_Alt-10 31675 37298 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_AltFn
Benchmark_Or_AltFn-10 4438 271606 ns/op 2876096 B/op 248 allocs/op
Benchmark_Or_AltFn-10 4713 263059 ns/op 2876096 B/op 248 allocs/op
Benchmark_Or_Buf
Benchmark_Or_Buf-10 39303 30316 ns/op 109376 B/op 8 allocs/op
Benchmark_Or_BufFn
Benchmark_Or_BufFn-10 4801 262440 ns/op 2876096 B/op 248 allocs/op
42 changes: 36 additions & 6 deletions bitmap_conc.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,31 @@ func OrAlt(a, b *Bitmap) *Bitmap {
return a.Clone()
}

buf := make([]uint16, maxContainerSize)
orContainersStandalone(a, b, res, buf)
return res
}

func OrBuf(a, b *Bitmap, buf []uint16) *Bitmap {
assert(len(buf) == maxContainerSize)

res := NewBitmap()
if ae, be := a.IsEmpty(), b.IsEmpty(); ae && be {
return res
} else if ae {
return b.Clone()
} else if be {
return a.Clone()
}

orContainersStandalone(a, b, res, buf)
return res
}

func orContainersStandalone(a, b, res *Bitmap, buf []uint16) {
ai, an := 0, a.keys.numKeys()
bi, bn := 0, b.keys.numKeys()

buf := make([]uint16, maxContainerSize)
for ai < an && bi < bn {
ak := a.keys.key(ai)
bk := b.keys.key(bi)
Expand Down Expand Up @@ -351,25 +372,34 @@ func OrAlt(a, b *Bitmap) *Bitmap {
res.setKey(bk, offset)
}
}

return res
}

func (ra *Bitmap) OrAlt(bm *Bitmap) *Bitmap {
if bm.IsEmpty() {
return ra
}

orContainersInRangeAlt(ra, bm, 0, bm.keys.numKeys())
buf := make([]uint16, maxContainerSize)
orContainersInRangeAlt(ra, bm, 0, bm.keys.numKeys(), buf)
return ra
}

func orContainersInRangeAlt(a, b *Bitmap, bi, bn int) {
func (ra *Bitmap) OrBuf(bm *Bitmap, buf []uint16) *Bitmap {
assert(len(buf) == maxContainerSize)

if bm.IsEmpty() {
return ra
}

orContainersInRangeAlt(ra, bm, 0, bm.keys.numKeys(), buf)
return ra
}

func orContainersInRangeAlt(a, b *Bitmap, bi, bn int, buf []uint16) {
bk := b.keys.key(bi)
ai := a.keys.search(bk)
an := a.keys.numKeys()

buf := make([]uint16, maxContainerSize)
for ai < an && bi < bn {
ak := a.keys.key(ai)
bk := b.keys.key(bi)
Expand Down
34 changes: 34 additions & 0 deletions bitmap_conc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,40 @@ func Benchmark_Or_AltFn(b *testing.B) {
}
}

// go test -v -bench Benchmark_Or_Buf -benchmem -run ^$ github.com/weaviate/sroar -cpuprofile cpu.prof
func Benchmark_Or_Buf(b *testing.B) {
for i := 0; i < b.N; i++ {
buf := make([]uint16, maxContainerSize)
s1 := superset.Clone()
s2 := superset.Clone()
b1 := bigset.Clone()
b2 := bigset.Clone()
for j, l := 0, len(subsets); j < l; j++ {
s1.OrBuf(subsets[j], buf)
s2.OrBuf(subsets[l-j-1], buf)
b1.OrBuf(subsets[j], buf)
b2.OrBuf(subsets[l-j-1], buf)
}
}
}

// go test -v -bench Benchmark_Or_BufFn -benchmem -run ^$ github.com/weaviate/sroar -cpuprofile cpu.prof
func Benchmark_Or_BufFn(b *testing.B) {
for i := 0; i < b.N; i++ {
buf := make([]uint16, maxContainerSize)
s1 := superset.Clone()
s2 := superset.Clone()
b1 := bigset.Clone()
b2 := bigset.Clone()
for j, l := 0, len(subsets); j < l; j++ {
s1 = OrBuf(s1, subsets[j], buf)
s2 = OrBuf(s2, subsets[l-j-1], buf)
b1 = OrBuf(b1, subsets[j], buf)
b2 = OrBuf(b2, subsets[l-j-1], buf)
}
}
}

// var bm *Bitmap
// var control []uint64
// var controls [][]uint64
Expand Down

0 comments on commit cb81762

Please sign in to comment.