From 935226b047f8311a72c432587ed3fac68786429c Mon Sep 17 00:00:00 2001 From: Andrew Ayer Date: Thu, 16 Feb 2023 20:27:20 -0500 Subject: [PATCH] merkletree: use math/bits.OnesCount64 for efficiency --- merkletree/collapsed_tree.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/merkletree/collapsed_tree.go b/merkletree/collapsed_tree.go index e138207..d0525bd 100644 --- a/merkletree/collapsed_tree.go +++ b/merkletree/collapsed_tree.go @@ -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 {