Speed up counting of bits in allocator

Benchmark:

goos: linux
goarch: amd64
pkg: k8s.io/kubernetes/pkg/registry/core/service/allocator
cpu: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz

Before:

BenchmarkCountBits-8     9459236               140.4 ns/op

After:

BenchmarkCountBits-8    140667842                9.541 ns/op
This commit is contained in:
Tom Payne 2021-10-01 17:09:26 +02:00
parent 125312a8cf
commit 21755f9ec0

View File

@ -24,8 +24,8 @@ import (
// countBits returns the number of set bits in n
func countBits(n *big.Int) int {
var count int = 0
for _, b := range n.Bytes() {
count += bits.OnesCount8(uint8(b))
for _, w := range n.Bits() {
count += bits.OnesCount64(uint64(w))
}
return count
}