From 5b6a6e49865664161fbd7b99eee698867628382b Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 11 Nov 2024 13:41:13 +0100 Subject: [PATCH] trie: make stacktrie use less alloc:y encoders --- trie/stacktrie.go | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/trie/stacktrie.go b/trie/stacktrie.go index d194cbf0ae..d66bba3f11 100644 --- a/trie/stacktrie.go +++ b/trie/stacktrie.go @@ -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: