Skip to content

Commit

Permalink
Move a line outside for-loop in readSinglePayload
Browse files Browse the repository at this point in the history
This optimization improves speed by 0.49% on Haswell CPU.

Thanks for suggestion @tarakby.

Co-authored-by: Tarak Ben Youssef <[email protected]>
  • Loading branch information
fxamacker and Tarak Ben Youssef committed May 25, 2022
1 parent d9a57fa commit c8b2bc5
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ledger/complete/mtrie/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,21 @@ func (mt *MTrie) ReadSinglePayload(path ledger.Path) *ledger.Payload {
func readSinglePayload(path ledger.Path, head *node.Node) *ledger.Payload {
pathBytes := path[:]

if head == nil {
return ledger.EmptyPayload()
}

depth := ledger.NodeMaxHeight - head.Height() // distance to the tree root

// Traverse nodes following the path until a leaf node or nil node is reached.
for !head.IsLeaf() {
depth := ledger.NodeMaxHeight - head.Height() // distance to the tree root
bit := bitutils.ReadBit(pathBytes, depth)
if bit == 0 {
head = head.LeftChild()
} else {
head = head.RightChild()
}
depth++
}

if head != nil && *head.Path() == path {
Expand Down

0 comments on commit c8b2bc5

Please sign in to comment.