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

Avoiding bound checks in setunion code #301

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all 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
33 changes: 18 additions & 15 deletions setutil_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

package roaring


func union2by2(set1 []uint16, set2 []uint16, buffer []uint16) int {
pos := 0
k1 := 0
k2 := 0
pos := uint(0)
k1 := uint(0)
k2 := uint(0)
len1 := uint(len(set1))
len2 := uint(len(set2))
if 0 == len(set2) {
buffer = buffer[:len(set1)]
copy(buffer, set1[:])
Expand All @@ -18,31 +21,31 @@ func union2by2(set1 []uint16, set2 []uint16, buffer []uint16) int {
}
s1 := set1[k1]
s2 := set2[k2]
buffer = buffer[:cap(buffer)]
for {
buffer = buffer[:len1 + len2]
for pos < uint(len(buffer)) {
if s1 < s2 {
buffer[pos] = s1
pos++
k1++
if k1 >= len(set1) {
if k1 >= len1 {
copy(buffer[pos:], set2[k2:])
pos += len(set2) - k2
break
pos += len2 - k2
return int(pos)
}
s1 = set1[k1]
} else if s1 == s2 {
buffer[pos] = s1
pos++
k1++
k2++
if k1 >= len(set1) {
if k1 >= len1 {
copy(buffer[pos:], set2[k2:])
pos += len(set2) - k2
pos += len2 - k2
break
}
if k2 >= len(set2) {
if k2 >= len2 {
copy(buffer[pos:], set1[k1:])
pos += len(set1) - k1
pos += len1 - k1
break
}
s1 = set1[k1]
Expand All @@ -51,13 +54,13 @@ func union2by2(set1 []uint16, set2 []uint16, buffer []uint16) int {
buffer[pos] = s2
pos++
k2++
if k2 >= len(set2) {
if k2 >= len2 {
copy(buffer[pos:], set1[k1:])
pos += len(set1) - k1
pos += len1 - k1
break
}
s2 = set2[k2]
}
}
return pos
return int(pos)
}