From 1cfe3f872f01fc2606e9f741419e54c743a048bf Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 16 Mar 2021 21:08:19 +0200 Subject: [PATCH] lib/uint64set: optimize bucket16.addMulti a bit --- lib/uint64set/uint64set.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/uint64set/uint64set.go b/lib/uint64set/uint64set.go index c4cdc0d306..a52d017e4d 100644 --- a/lib/uint64set/uint64set.go +++ b/lib/uint64set/uint64set.go @@ -854,23 +854,27 @@ func (b *bucket16) add(x uint16) bool { func (b *bucket16) addMulti(a []uint64) int { count := 0 - bits := b.bits - if bits == nil { + if b.bits == nil { // Slow path - for _, x := range a { + for i, x := range a { if b.add(uint16(x)) { count++ } - } - } else { - // Fast path - for _, x := range a { - wordNum, bitMask := getWordNumBitMask(uint16(x)) - if bits[wordNum]&bitMask == 0 { - bits[wordNum] |= bitMask - count++ + if b.bits != nil { + a = a[i+1:] + goto fastPath } } + return count + } +fastPath: + bits := b.bits + for _, x := range a { + wordNum, bitMask := getWordNumBitMask(uint16(x)) + if bits[wordNum]&bitMask == 0 { + bits[wordNum] |= bitMask + count++ + } } return count }