From cd81a9a33f9a9321e92b591a0b007e16fd6e3e27 Mon Sep 17 00:00:00 2001 From: Seebs Date: Wed, 20 Mar 2019 08:58:58 -0500 Subject: [PATCH] hint to the bounds checker for bitmapRepair You might wonder why `i <= bitmapN-4`. Answer: The compiler isn't smart enough for the stride analysis to figure out that `i <= bitmapN` actually guarantees that. If you set the limit to something not a multiple of stride, though, it can't figure out *anything* about things. But for some reason, `i < bitmapN-3` fails badly (it actually adds bounds checks not present with `i < bitmapN`), but `i <= bitmapN - 4` works. This reduces runtime of bitmapRepair by about 14%. --- roaring/roaring.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roaring/roaring.go b/roaring/roaring.go index 952e988ad..98eb7b5de 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -2255,8 +2255,8 @@ func (c *Container) bitmapRepair() { // Manually unroll loop to make it a little faster. // TODO(rartoul): Can probably make this a few x faster using // SIMD instructions. - bitmap := c.bitmap() - for i := 0; i < bitmapN; i += 4 { + bitmap := c.bitmap()[:bitmapN] + for i := 0; i <= bitmapN-4; i += 4 { n += int32(popcount(bitmap[i])) n += int32(popcount(bitmap[i+1])) n += int32(popcount(bitmap[i+2]))