Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize hashing of leaf nodes #2894

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading