From 3435212d04a5dad5187eafced07541083e7c469d Mon Sep 17 00:00:00 2001 From: Seebs Date: Tue, 4 May 2021 14:26:50 -0500 Subject: [PATCH] gratuitously fancy logic for array/array callbacks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When searching for a small array in a large array, scanning ahead is productive. The switch from counting indexes to reslicing the slice appears to improve performance in this case. The fairly arbitrary value `na << 2` is like `nb / 4 > na` except that it computes faster, and lets us avoid the expensive overhead unless we have reason to expect that there's significantly more items in b than in a. Improvements: Not huge in some cases, but sometimes quite noticeable, especially as the frequency of overlap increases, which is also the expensive case in other ways. name old time/op new time/op delta ImportMutexSampleData/64K/2Kr/40/none/write-0-8 501ms ± 4% 486ms ± 2% ~ (p=0.052 n=6+5) ImportMutexSampleData/64K/2Kr/40/none/write-1-8 756ms ± 5% 698ms ± 5% -7.62% (p=0.002 n=6+6) ImportMutexSampleData/64K/2Kr/80/none/write-0-8 292ms ± 3% 276ms ± 4% -5.46% (p=0.002 n=6+6) ImportMutexSampleData/64K/2Kr/80/none/write-1-8 511ms ± 6% 482ms ± 4% -5.72% (p=0.015 n=6+6) ImportMutexSampleData/64K/2Kr/240/none/write-0-8 153ms ± 3% 132ms ± 5% -13.91% (p=0.008 n=5+5) ImportMutexSampleData/64K/2Kr/240/none/write-1-8 354ms ± 2% 215ms ± 6% -39.41% (p=0.004 n=5+6) ImportMutexSampleData/1K/2Kr/40/none/write-0-8 565ms ± 3% 543ms ± 3% -3.89% (p=0.015 n=6+6) ImportMutexSampleData/1K/2Kr/40/none/write-1-8 807ms ± 6% 778ms ± 3% ~ (p=0.180 n=6+6) ImportMutexSampleData/1K/2Kr/80/none/write-0-8 317ms ± 3% 300ms ± 1% -5.40% (p=0.002 n=6+6) ImportMutexSampleData/1K/2Kr/80/none/write-1-8 462ms ± 3% 437ms ± 4% -5.31% (p=0.009 n=6+6) ImportMutexSampleData/1K/2Kr/240/none/write-0-8 141ms ± 1% 119ms ± 2% -15.85% (p=0.004 n=5+6) ImportMutexSampleData/1K/2Kr/240/none/write-1-8 213ms ± 3% 171ms ± 3% -19.70% (p=0.002 n=6+6) --- roaring/roaring.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/roaring/roaring.go b/roaring/roaring.go index d7f632a0e..5504b9bf1 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -4445,6 +4445,23 @@ func intersectionCallbackArrayArray(a, b *Container, fn func(uint16)) { ca, cb = cb, ca na, nb = nb, na // nolint: staticcheck, ineffassign } + if (na << 2) < nb { + for _, va := range ca { + for cb[0] < va { + if len(cb) > 8 && cb[0] < va { + cb = cb[8:] + } + cb = cb[1:] + if len(cb) == 0 { + return + } + } + if cb[0] == va { + fn(va) + } + } + return + } j := 0 for _, va := range ca { for cb[j] < va {