Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(query): Improve Intersection with UID pack #8941

Merged
merged 6 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions algo/uidlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,34 +105,45 @@ func IntersectCompressedWithBin(dec *codec.Decoder, q []uint64, o *[]uint64) {

// Pick the shorter list and do binary search
if ld < lq {
uids := dec.Uids()
for len(uids) > 0 {
for _, u := range uids {
qidx := sort.Search(len(q), func(idx int) bool {
return q[idx] >= u
})
if qidx >= len(q) {
return
}
if q[qidx] == u {
*o = append(*o, u)
qidx++
}
q = q[qidx:]
for {
block_uids := dec.Uids()
mangalaman93 marked this conversation as resolved.
Show resolved Hide resolved
if len(block_uids) == 0 {
break
}
uids = dec.Next()
IntersectWithBin(block_uids, q, o)
last_uid := block_uids[len(block_uids)-1]
qidx := sort.Search(len(q), func(idx int) bool {
return q[idx] >= last_uid
})
if qidx >= len(q) {
return
}
q = q[qidx:]
mangalaman93 marked this conversation as resolved.
Show resolved Hide resolved
dec.Next()
}
return
}

uids := dec.Seek(q[0], codec.SeekStart)
mangalaman93 marked this conversation as resolved.
Show resolved Hide resolved
for _, u := range q {
uids := dec.Seek(u, codec.SeekStart)
if len(uids) == 0 {
return
if len(uids) == 0 || u > uids[len(uids)-1] {
uids = dec.Seek(u, codec.SeekStart)
if len(uids) == 0 {
return
}
}
uidIdx := sort.Search(len(uids), func(idx int) bool {
return uids[idx] >= u
})
if uidIdx >= len(uids) {
// We know that u < max(uids). If we didn't find it here, it's not here.
continue
}
if uids[0] == u {
if uids[uidIdx] == u {
*o = append(*o, u)
uidIdx++
}
uids = uids[uidIdx:]
}
}

Expand Down
46 changes: 46 additions & 0 deletions algo/uidlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,52 @@ func BenchmarkListIntersectRandom(b *testing.B) {
randomTests(1024000, 0.01)
}

func BenchmarkListIntersectCompressBin(b *testing.B) {
randomTests := func(sz int, overlap float64) {
rs := []float64{0.01, 0.1, 1, 10, 100}
for _, r := range rs {
sz1 := sz
sz2 := int(float64(sz) * r)
if sz2 > 1000000 || sz2 == 0 {
break
}

u1, v1 := make([]uint64, sz1), make([]uint64, sz2)
limit := int64(float64(sz) / overlap)
for i := 0; i < sz1; i++ {
u1[i] = uint64(rand.Int63n(limit))
}
for i := 0; i < sz2; i++ {
v1[i] = uint64(rand.Int63n(limit))
}
sort.Slice(u1, func(i, j int) bool { return u1[i] < u1[j] })
sort.Slice(v1, func(i, j int) bool { return v1[i] < v1[j] })

dst2 := &pb.List{}
compressedUids := codec.Encode(v1, 256)

b.Run(fmt.Sprintf("compressed:IntersectWith:ratio=%v:size=%d:overlap=%.2f:", r, sz, overlap),
func(b *testing.B) {
for k := 0; k < b.N; k++ {
dec := codec.Decoder{Pack: compressedUids}
dec.Seek(0, codec.SeekStart)
IntersectCompressedWithBin(&dec, u1, &dst2.Uids)
}
})
fmt.Println()

codec.FreePack(compressedUids)
}
}

randomTests(10, 0.01)
randomTests(100, 0.01)
randomTests(1000, 0.01)
randomTests(10000, 0.01)
randomTests(100000, 0.01)
randomTests(1000000, 0.01)
}

func BenchmarkListIntersectRatio(b *testing.B) {
randomTests := func(sz int, overlap float64) {
rs := []int{1, 10, 50, 100, 500, 1000, 10000, 100000, 1000000}
Expand Down