From d4364bea527f3a7d023e41974dc97b2c2ef8cc13 Mon Sep 17 00:00:00 2001 From: Seebs Date: Mon, 12 Nov 2018 18:04:54 -0600 Subject: [PATCH] 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. --- roaring/roaring.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/roaring/roaring.go b/roaring/roaring.go index ced003d37..6cf7eda1c 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -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