trie: make stacktrie use less alloc:y encoders

This commit is contained in:
Martin Holst Swende 2024-11-11 13:41:13 +01:00
parent c7f6aec7db
commit 5b6a6e4986
No known key found for this signature in database
GPG Key ID: 683B438C05A5DDF0
1 changed files with 12 additions and 16 deletions

View File

@ -318,19 +318,13 @@ func (t *StackTrie) hash(st *stNode, path []byte) {
return
case branchNode:
var nodes fullNode
var nodes fullnodeEncoder
for i, child := range st.children {
if child == nil {
nodes.Children[i] = nilValueNode
continue
}
t.hash(child, append(path, byte(i)))
if len(child.val) < 32 {
nodes.Children[i] = rawNode(child.val)
} else {
nodes.Children[i] = hashNode(child.val)
}
nodes.Children[i] = child.val
st.children[i] = nil
stPool.Put(child.reset()) // Release child back to pool.
}
@ -342,11 +336,9 @@ func (t *StackTrie) hash(st *stNode, path []byte) {
t.hash(st.children[0], append(path, st.key...))
// encode the extension node
n := shortNode{Key: hexToCompactInPlace(st.key)}
if len(st.children[0].val) < 32 {
n.Val = rawNode(st.children[0].val)
} else {
n.Val = hashNode(st.children[0].val)
n := shortNodeEncoder{
Key: hexToCompactInPlace(st.key),
Val: st.children[0].val,
}
n.encode(t.h.encbuf)
blob = t.h.encodedBytes()
@ -356,9 +348,13 @@ func (t *StackTrie) hash(st *stNode, path []byte) {
case leafNode:
st.key = append(st.key, byte(16))
n := shortNode{Key: hexToCompactInPlace(st.key), Val: valueNode(st.val)}
n.encode(t.h.encbuf)
{
w := t.h.encbuf
offset := w.List()
w.WriteBytes(hexToCompactInPlace(st.key))
w.WriteBytes(st.val)
w.ListEnd(offset)
}
blob = t.h.encodedBytes()
default: