slightly streamline array/array comparison

The net effect of this is to not recompute "the current
value of the first array" on every loop, pretty much.
However, the swap to make sure the inner loop is on the
longer array seems to be significant for performance.
On my system, this moves runtime from ~29us per op
to ~17us per op.
This commit is contained in:
Seebs 2018-11-12 18:04:54 -06:00
parent 32c4b3540f
commit d4364bea52

View file

@ -1903,16 +1903,26 @@ func intersectionCount(a, b *Container) int32 {
func intersectionCountArrayArray(a, b *Container) (n int32) {
statsHit("intersectionCount/ArrayArray")
na, nb := len(a.array), len(b.array)
for i, j := 0, 0; i < na && j < nb; {
va, vb := a.array[i], b.array[j]
if va < vb {
i++
} else if va > vb {
j++
} else {
s1, s2 := a.array, b.array
if len(s1) == 0 || len(s2) == 0 {
return 0
}
if len(s1) > len(s2) {
s1, s2 = s2, s1
}
l2 := len(s2)
i2 := 0
v2 := s2[0]
for _, v1 := range s1 {
for v2 < v1 {
i2++
if i2 >= l2 {
return n
}
v2 = s2[i2]
}
if v2 == v1 {
n++
i, j = i+1, j+1
}
}
return n