merkletree: use math/bits.OnesCount64 for efficiency

This commit is contained in:
Andrew Ayer 2023-02-16 20:27:20 -05:00
parent 4ca81ab8aa
commit 935226b047
1 changed files with 2 additions and 6 deletions

View File

@ -12,6 +12,7 @@ package merkletree
import (
"encoding/json"
"fmt"
"math/bits"
)
type CollapsedTree struct {
@ -20,12 +21,7 @@ type CollapsedTree struct {
}
func calculateNumNodes(size uint64) int {
numNodes := 0
for size > 0 {
numNodes += int(size & 1)
size >>= 1
}
return numNodes
return bits.OnesCount64(size)
}
func EmptyCollapsedTree() *CollapsedTree {