Skip to content

Commit cb81762

Browse files
committed
orbuf method/fn
1 parent 49772c9 commit cb81762

File tree

3 files changed

+127
-35
lines changed

3 files changed

+127
-35
lines changed

benchmark_or.txt

+57-29
Original file line numberDiff line numberDiff line change
@@ -4,94 +4,122 @@ go test -v -bench Benchmark_Or_ -benchmem -run ^$ github.com/weaviate/sroar
44
elements: 7
55

66
Benchmark_Or_Old
7-
Benchmark_Or_Old-10 124358 8744 ns/op 2240 B/op 8 allocs/op
7+
Benchmark_Or_Old-10 137358 8619 ns/op 2240 B/op 8 allocs/op
88
Benchmark_Or_OldFn
9-
Benchmark_Or_OldFn-10 64314 19737 ns/op 35520 B/op 178 allocs/op
9+
Benchmark_Or_OldFn-10 63716 18401 ns/op 35520 B/op 178 allocs/op
1010
Benchmark_Or_Alt
11-
Benchmark_Or_Alt-10 193574 5896 ns/op 2240 B/op 8 allocs/op
11+
Benchmark_Or_Alt-10 193772 5872 ns/op 2240 B/op 8 allocs/op
1212
Benchmark_Or_AltFn
13-
Benchmark_Or_AltFn-10 75320 16180 ns/op 49984 B/op 208 allocs/op
13+
Benchmark_Or_AltFn-10 71002 16506 ns/op 49984 B/op 208 allocs/op
14+
Benchmark_Or_Buf
15+
Benchmark_Or_Buf-10 666516 1710 ns/op 2240 B/op 8 allocs/op
16+
Benchmark_Or_BufFn
17+
Benchmark_Or_BufFn-10 98116 11774 ns/op 49984 B/op 208 allocs/op
1418

1519

1620

1721

1822
elements: 67
1923

2024
Benchmark_Or_Old
21-
Benchmark_Or_Old-10 73114 16408 ns/op 7936 B/op 12 allocs/op
25+
Benchmark_Or_Old-10 71934 15913 ns/op 7936 B/op 12 allocs/op
2226
Benchmark_Or_OldFn
23-
Benchmark_Or_OldFn-10 45784 25726 ns/op 60032 B/op 210 allocs/op
27+
Benchmark_Or_OldFn-10 45294 26045 ns/op 60032 B/op 210 allocs/op
2428
Benchmark_Or_Alt
25-
Benchmark_Or_Alt-10 80880 14287 ns/op 4864 B/op 10 allocs/op
29+
Benchmark_Or_Alt-10 81104 14267 ns/op 4864 B/op 10 allocs/op
2630
Benchmark_Or_AltFn
27-
Benchmark_Or_AltFn-10 44790 26830 ns/op 66112 B/op 209 allocs/op
31+
Benchmark_Or_AltFn-10 44512 26713 ns/op 66112 B/op 209 allocs/op
32+
Benchmark_Or_Buf
33+
Benchmark_Or_Buf-10 152304 7368 ns/op 4864 B/op 10 allocs/op
34+
Benchmark_Or_BufFn
35+
Benchmark_Or_BufFn-10 59793 19780 ns/op 66112 B/op 209 allocs/op
2836

2937

3038

3139

3240
elements: 567
3341

3442
Benchmark_Or_Old
35-
Benchmark_Or_Old-10 16183 72031 ns/op 36928 B/op 12 allocs/op
43+
Benchmark_Or_Old-10 16530 70862 ns/op 36928 B/op 12 allocs/op
3644
Benchmark_Or_OldFn
37-
Benchmark_Or_OldFn-10 9369 109007 ns/op 388032 B/op 248 allocs/op
45+
Benchmark_Or_OldFn-10 11068 109576 ns/op 388032 B/op 248 allocs/op
3846
Benchmark_Or_Alt
39-
Benchmark_Or_Alt-10 16832 70811 ns/op 36928 B/op 12 allocs/op
47+
Benchmark_Or_Alt-10 16742 71016 ns/op 36928 B/op 12 allocs/op
4048
Benchmark_Or_AltFn
41-
Benchmark_Or_AltFn-10 9037 117504 ns/op 566656 B/op 248 allocs/op
49+
Benchmark_Or_AltFn-10 8902 117368 ns/op 566656 B/op 248 allocs/op
50+
Benchmark_Or_Buf
51+
Benchmark_Or_Buf-10 18942 63016 ns/op 36928 B/op 12 allocs/op
52+
Benchmark_Or_BufFn
53+
Benchmark_Or_BufFn-10 9580 109518 ns/op 566656 B/op 248 allocs/op
4254

4355

4456

4557

4658
elements: 4567
4759

4860
Benchmark_Or_Old
49-
Benchmark_Or_Old-10 8365 125783 ns/op 136512 B/op 10 allocs/op
61+
Benchmark_Or_Old-10 10142 123281 ns/op 136512 B/op 10 allocs/op
5062
Benchmark_Or_OldFn
51-
Benchmark_Or_OldFn-10 2625 435354 ns/op 2429763 B/op 248 allocs/op
63+
Benchmark_Or_OldFn-10 2654 435234 ns/op 2429761 B/op 248 allocs/op
5264
Benchmark_Or_Alt
53-
Benchmark_Or_Alt-10 9212 121773 ns/op 136512 B/op 10 allocs/op
65+
Benchmark_Or_Alt-10 9439 121141 ns/op 136512 B/op 10 allocs/op
5466
Benchmark_Or_AltFn
55-
Benchmark_Or_AltFn-10 3561 312373 ns/op 2740417 B/op 248 allocs/op
67+
Benchmark_Or_AltFn-10 3621 316071 ns/op 2740417 B/op 248 allocs/op
68+
Benchmark_Or_Buf
69+
Benchmark_Or_Buf-10 9802 114179 ns/op 136512 B/op 10 allocs/op
70+
Benchmark_Or_BufFn
71+
Benchmark_Or_BufFn-10 3571 321915 ns/op 2740417 B/op 248 allocs/op
5672

5773

5874

5975

6076
elements: 34567
6177

6278
Benchmark_Or_Old
63-
Benchmark_Or_Old-10 4281 278046 ns/op 109376 B/op 8 allocs/op
79+
Benchmark_Or_Old-10 4275 279616 ns/op 109376 B/op 8 allocs/op
6480
Benchmark_Or_OldFn
65-
Benchmark_Or_OldFn-10 2444 470096 ns/op 2876097 B/op 248 allocs/op
81+
Benchmark_Or_OldFn-10 2479 475768 ns/op 2876098 B/op 248 allocs/op
6682
Benchmark_Or_Alt
67-
Benchmark_Or_Alt-10 4057 277929 ns/op 109376 B/op 8 allocs/op
83+
Benchmark_Or_Alt-10 4068 284528 ns/op 109376 B/op 8 allocs/op
6884
Benchmark_Or_AltFn
69-
Benchmark_Or_AltFn-10 2379 498199 ns/op 2876097 B/op 248 allocs/op
85+
Benchmark_Or_AltFn-10 2395 477734 ns/op 2876098 B/op 248 allocs/op
86+
Benchmark_Or_Buf
87+
Benchmark_Or_Buf-10 4262 272442 ns/op 109376 B/op 8 allocs/op
88+
Benchmark_Or_BufFn
89+
Benchmark_Or_BufFn-10 2414 470769 ns/op 2876100 B/op 248 allocs/op
7090

7191

7292

7393

7494
elements: 234567
7595

7696
Benchmark_Or_Old
77-
Benchmark_Or_Old-10 3514 331398 ns/op 109377 B/op 8 allocs/op
97+
Benchmark_Or_Old-10 3523 345830 ns/op 109377 B/op 8 allocs/op
7898
Benchmark_Or_OldFn
79-
Benchmark_Or_OldFn-10 2322 511811 ns/op 2876098 B/op 248 allocs/op
99+
Benchmark_Or_OldFn-10 2230 545129 ns/op 2876100 B/op 248 allocs/op
80100
Benchmark_Or_Alt
81-
Benchmark_Or_Alt-10 10000 114039 ns/op 109376 B/op 8 allocs/op
101+
Benchmark_Or_Alt-10 10000 114571 ns/op 109376 B/op 8 allocs/op
82102
Benchmark_Or_AltFn
83-
Benchmark_Or_AltFn-10 3792 333178 ns/op 2876096 B/op 248 allocs/op
84-
103+
Benchmark_Or_AltFn-10 3700 337825 ns/op 2876097 B/op 248 allocs/op
104+
Benchmark_Or_Buf
105+
Benchmark_Or_Buf-10 10000 106827 ns/op 109376 B/op 8 allocs/op
106+
Benchmark_Or_BufFn
107+
Benchmark_Or_BufFn-10 3768 320828 ns/op 2876096 B/op 248 allocs/op
85108

86109

87110

88111
elements: 1234567
89112

113+
cpu: Apple M1 Pro
90114
Benchmark_Or_Old
91-
Benchmark_Or_Old-10 14146 83943 ns/op 109376 B/op 8 allocs/op
115+
Benchmark_Or_Old-10 14138 84844 ns/op 109376 B/op 8 allocs/op
92116
Benchmark_Or_OldFn
93-
Benchmark_Or_OldFn-10 3848 319742 ns/op 2876096 B/op 248 allocs/op
117+
Benchmark_Or_OldFn-10 4010 320525 ns/op 2876096 B/op 248 allocs/op
94118
Benchmark_Or_Alt
95-
Benchmark_Or_Alt-10 31942 37684 ns/op 109376 B/op 8 allocs/op
119+
Benchmark_Or_Alt-10 31675 37298 ns/op 109376 B/op 8 allocs/op
96120
Benchmark_Or_AltFn
97-
Benchmark_Or_AltFn-10 4438 271606 ns/op 2876096 B/op 248 allocs/op
121+
Benchmark_Or_AltFn-10 4713 263059 ns/op 2876096 B/op 248 allocs/op
122+
Benchmark_Or_Buf
123+
Benchmark_Or_Buf-10 39303 30316 ns/op 109376 B/op 8 allocs/op
124+
Benchmark_Or_BufFn
125+
Benchmark_Or_BufFn-10 4801 262440 ns/op 2876096 B/op 248 allocs/op

bitmap_conc.go

+36-6
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,31 @@ func OrAlt(a, b *Bitmap) *Bitmap {
287287
return a.Clone()
288288
}
289289

290+
buf := make([]uint16, maxContainerSize)
291+
orContainersStandalone(a, b, res, buf)
292+
return res
293+
}
294+
295+
func OrBuf(a, b *Bitmap, buf []uint16) *Bitmap {
296+
assert(len(buf) == maxContainerSize)
297+
298+
res := NewBitmap()
299+
if ae, be := a.IsEmpty(), b.IsEmpty(); ae && be {
300+
return res
301+
} else if ae {
302+
return b.Clone()
303+
} else if be {
304+
return a.Clone()
305+
}
306+
307+
orContainersStandalone(a, b, res, buf)
308+
return res
309+
}
310+
311+
func orContainersStandalone(a, b, res *Bitmap, buf []uint16) {
290312
ai, an := 0, a.keys.numKeys()
291313
bi, bn := 0, b.keys.numKeys()
292314

293-
buf := make([]uint16, maxContainerSize)
294315
for ai < an && bi < bn {
295316
ak := a.keys.key(ai)
296317
bk := b.keys.key(bi)
@@ -351,25 +372,34 @@ func OrAlt(a, b *Bitmap) *Bitmap {
351372
res.setKey(bk, offset)
352373
}
353374
}
354-
355-
return res
356375
}
357376

358377
func (ra *Bitmap) OrAlt(bm *Bitmap) *Bitmap {
359378
if bm.IsEmpty() {
360379
return ra
361380
}
362381

363-
orContainersInRangeAlt(ra, bm, 0, bm.keys.numKeys())
382+
buf := make([]uint16, maxContainerSize)
383+
orContainersInRangeAlt(ra, bm, 0, bm.keys.numKeys(), buf)
364384
return ra
365385
}
366386

367-
func orContainersInRangeAlt(a, b *Bitmap, bi, bn int) {
387+
func (ra *Bitmap) OrBuf(bm *Bitmap, buf []uint16) *Bitmap {
388+
assert(len(buf) == maxContainerSize)
389+
390+
if bm.IsEmpty() {
391+
return ra
392+
}
393+
394+
orContainersInRangeAlt(ra, bm, 0, bm.keys.numKeys(), buf)
395+
return ra
396+
}
397+
398+
func orContainersInRangeAlt(a, b *Bitmap, bi, bn int, buf []uint16) {
368399
bk := b.keys.key(bi)
369400
ai := a.keys.search(bk)
370401
an := a.keys.numKeys()
371402

372-
buf := make([]uint16, maxContainerSize)
373403
for ai < an && bi < bn {
374404
ak := a.keys.key(ai)
375405
bk := b.keys.key(bi)

bitmap_conc_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,40 @@ func Benchmark_Or_AltFn(b *testing.B) {
249249
}
250250
}
251251

252+
// go test -v -bench Benchmark_Or_Buf -benchmem -run ^$ github.com/weaviate/sroar -cpuprofile cpu.prof
253+
func Benchmark_Or_Buf(b *testing.B) {
254+
for i := 0; i < b.N; i++ {
255+
buf := make([]uint16, maxContainerSize)
256+
s1 := superset.Clone()
257+
s2 := superset.Clone()
258+
b1 := bigset.Clone()
259+
b2 := bigset.Clone()
260+
for j, l := 0, len(subsets); j < l; j++ {
261+
s1.OrBuf(subsets[j], buf)
262+
s2.OrBuf(subsets[l-j-1], buf)
263+
b1.OrBuf(subsets[j], buf)
264+
b2.OrBuf(subsets[l-j-1], buf)
265+
}
266+
}
267+
}
268+
269+
// go test -v -bench Benchmark_Or_BufFn -benchmem -run ^$ github.com/weaviate/sroar -cpuprofile cpu.prof
270+
func Benchmark_Or_BufFn(b *testing.B) {
271+
for i := 0; i < b.N; i++ {
272+
buf := make([]uint16, maxContainerSize)
273+
s1 := superset.Clone()
274+
s2 := superset.Clone()
275+
b1 := bigset.Clone()
276+
b2 := bigset.Clone()
277+
for j, l := 0, len(subsets); j < l; j++ {
278+
s1 = OrBuf(s1, subsets[j], buf)
279+
s2 = OrBuf(s2, subsets[l-j-1], buf)
280+
b1 = OrBuf(b1, subsets[j], buf)
281+
b2 = OrBuf(b2, subsets[l-j-1], buf)
282+
}
283+
}
284+
}
285+
252286
// var bm *Bitmap
253287
// var control []uint64
254288
// var controls [][]uint64

0 commit comments

Comments
 (0)