merkletree: use math/bits.OnesCount64 for efficiency
This commit is contained in:
parent
4ca81ab8aa
commit
935226b047
|
@ -12,6 +12,7 @@ package merkletree
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/bits"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CollapsedTree struct {
|
type CollapsedTree struct {
|
||||||
|
@ -20,12 +21,7 @@ type CollapsedTree struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateNumNodes(size uint64) int {
|
func calculateNumNodes(size uint64) int {
|
||||||
numNodes := 0
|
return bits.OnesCount64(size)
|
||||||
for size > 0 {
|
|
||||||
numNodes += int(size & 1)
|
|
||||||
size >>= 1
|
|
||||||
}
|
|
||||||
return numNodes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func EmptyCollapsedTree() *CollapsedTree {
|
func EmptyCollapsedTree() *CollapsedTree {
|
||||||
|
|
Loading…
Reference in New Issue