Skip to content

Commit

Permalink
crypto/types: optimize compact bit array one count (#9216)
Browse files Browse the repository at this point in the history
By using bits.OnesCount8 instead of bitwise operations.

Benchmark result on Apple M1:

name                     old time/op    new time/op    delta
NumTrueBitsBefore/new-8    88.5ns ± 1%     9.1ns ± 0%  -89.68%  (p=0.000 n=10+10)

name                     old alloc/op   new alloc/op   delta
NumTrueBitsBefore/new-8     0.00B          0.00B          ~     (all equal)

name                     old allocs/op  new allocs/op  delta
NumTrueBitsBefore/new-8      0.00           0.00          ~     (all equal)

Fixes #9125
  • Loading branch information
Cuong Manh Le authored Apr 30, 2021
1 parent f04b5dc commit 72873a0
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions crypto/types/compact_bit_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"math"
"math/bits"
"regexp"
"strings"
)
Expand Down Expand Up @@ -85,23 +86,18 @@ func (bA *CompactBitArray) SetIndex(i int, v bool) bool {
// given index. e.g. if bA = _XX__XX, NumOfTrueBitsBefore(4) = 2, since
// there are two bits set to true before index 4.
func (bA *CompactBitArray) NumTrueBitsBefore(index int) int {
numTrueValues := 0
onesCount := 0
max := bA.Count()
if index > max {
index = max
}
// below we iterate over the bytes then over bits (in low endian) and count bits set to 1
var i = 0
for elem := 0; ; elem++ {
for b := 7; b >= 0; b-- {
if i >= index {
return numTrueValues
}
i++
if (bA.Elems[elem]>>b)&1 == 1 {
numTrueValues++
}
if elem*8+7 >= index {
onesCount += bits.OnesCount8(bA.Elems[elem] >> (7 - (index % 8) + 1))
return onesCount
}
onesCount += bits.OnesCount8(bA.Elems[elem])
}
}

Expand Down

0 comments on commit 72873a0

Please sign in to comment.