Skip to content

Commit

Permalink
Merge pull request FeatureBaseDB#577 from jaffee/intersectCountOptimi…
Browse files Browse the repository at this point in the history
…zation

rewrite intersectCountArrayBitmap for perf test
  • Loading branch information
jaffee authored May 30, 2017
2 parents f4466f9 + da961be commit c4fccc0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
14 changes: 13 additions & 1 deletion roaring/roaring.go
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ func intersectionCountArrayArray(a, b *container) (n uint64) {
return n
}

func intersectionCountArrayBitmap(a, b *container) (n uint64) {
func intersectionCountArrayBitmapOld(a, b *container) (n uint64) {
// Copy array header so we can shrink it.
array := a.array
if len(array) == 0 {
Expand Down Expand Up @@ -1270,6 +1270,18 @@ func intersectionCountArrayBitmap(a, b *container) (n uint64) {
return n
}

func intersectionCountArrayBitmap(a, b *container) (n uint64) {
for _, val := range a.array {
i := val / 64
if i >= uint32(len(b.bitmap)) {
break
}
off := val % 64
n += (b.bitmap[i] & (1 << off)) >> off
}
return n
}

func intersectionCountBitmapBitmap(a, b *container) (n uint64) {
return popcntAndSlice(a.bitmap, b.bitmap)
}
Expand Down
49 changes: 46 additions & 3 deletions roaring/roaring_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

package roaring

import (
"testing"
)
import "testing"

func TestBitmapCountRange(t *testing.T) {
c := container{}
Expand All @@ -41,3 +39,48 @@ func TestBitmapCountRange(t *testing.T) {
}
}
}

func TestIntersectionCountArrayBitmap2(t *testing.T) {
a, b := &container{}, &container{}
tests := []struct {
array []uint32
bitmap []uint64
exp uint64
}{
{
array: []uint32{0},
bitmap: []uint64{1},
exp: 1,
},
{
array: []uint32{0, 1},
bitmap: []uint64{3},
exp: 2,
},
{
array: []uint32{64, 128, 129, 2000},
bitmap: []uint64{932421, 2},
exp: 0,
},
{
array: []uint32{0, 65, 130, 195},
bitmap: []uint64{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
exp: 4,
},
{
array: []uint32{63, 120, 543, 639, 12000},
bitmap: []uint64{0x8000000000000000, 0, 0, 0, 0, 0, 0, 0, 0, 0x8000000000000000},
exp: 2,
},
}

for i, test := range tests {
a.array = test.array
b.bitmap = test.bitmap
ret1 := intersectionCountArrayBitmapOld(a, b)
ret2 := intersectionCountArrayBitmap(a, b)
if ret1 != ret2 || ret2 != test.exp {
t.Fatalf("test #%v intersectCountArrayBitmap fail orig: %v new: %v exp: %v", i, ret1, ret2, test.exp)
}
}
}

0 comments on commit c4fccc0

Please sign in to comment.