Skip to content

Commit

Permalink
Optimize hashing of leaf nodes (#2894)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Apr 2, 2024
1 parent 617a9e2 commit 00d4e0a
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions x/merkledb/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,27 @@ func hashNode(n *node) ids.ID {
// By directly calling sha.Write rather than passing sha around as an
// io.Writer, the compiler can perform sufficient escape analysis to avoid
// allocating buffers on the heap.
_, _ = sha.Write(binary.AppendUvarint(emptyHashBuffer, uint64(len(n.children))))

// By allocating BranchFactorLargest rather than len(n.children), this slice
// is allocated on the stack rather than the heap. BranchFactorLargest is
// at least len(n.children) which avoids memory allocations.
keys := make([]byte, 0, BranchFactorLargest)
for k := range n.children {
keys = append(keys, k)
}
numChildren := len(n.children)
_, _ = sha.Write(binary.AppendUvarint(emptyHashBuffer, uint64(numChildren)))

// Avoid allocating keys entirely if the node doesn't have any children.
if numChildren != 0 {
// By allocating BranchFactorLargest rather than len(n.children), this
// slice is allocated on the stack rather than the heap.
// BranchFactorLargest is at least len(n.children) which avoids memory
// allocations.
keys := make([]byte, 0, BranchFactorLargest)
for k := range n.children {
keys = append(keys, k)
}

// Ensure that the order of entries is correct.
slices.Sort(keys)
for _, index := range keys {
entry := n.children[index]
_, _ = sha.Write(binary.AppendUvarint(emptyHashBuffer, uint64(index)))
_, _ = sha.Write(entry.id[:])
// Ensure that the order of entries is correct.
slices.Sort(keys)
for _, index := range keys {
entry := n.children[index]
_, _ = sha.Write(binary.AppendUvarint(emptyHashBuffer, uint64(index)))
_, _ = sha.Write(entry.id[:])
}
}

if n.valueDigest.HasValue() {
Expand All @@ -136,7 +141,14 @@ func hashNode(n *node) ids.ID {
func encodeDBNode(n *dbNode) []byte {
buf := bytes.NewBuffer(make([]byte, 0, encodedDBNodeSize(n)))
encodeMaybeByteSlice(buf, n.value)
encodeUint(buf, uint64(len(n.children)))

numChildren := len(n.children)
encodeUint(buf, uint64(numChildren))

// Avoid allocating keys entirely if the node doesn't have any children.
if numChildren == 0 {
return buf.Bytes()
}

// By allocating BranchFactorLargest rather than len(n.children), this slice
// is allocated on the stack rather than the heap. BranchFactorLargest is
Expand Down

0 comments on commit 00d4e0a

Please sign in to comment.