From a7f2803f8e0eab7908b219b8b15996fd088f5281 Mon Sep 17 00:00:00 2001 From: maskpp Date: Sun, 7 Sep 2025 15:32:27 +0800 Subject: [PATCH] a small improvement to reduce gc about the latest field --- trie/encoding.go | 1 - trie/stacktrie.go | 9 +++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/trie/encoding.go b/trie/encoding.go index 4cd29f531a..7d50ee4f57 100644 --- a/trie/encoding.go +++ b/trie/encoding.go @@ -108,7 +108,6 @@ func keybytesToHex(str []byte) []byte { // OBS! This method omits the termination flag. // OBS! The dst slice must be at least 2x as large as the key func writeHexKey(dst []byte, key []byte) []byte { - _ = dst[2*len(key)-1] for i, b := range key { dst[i*2] = b / 16 dst[i*2+1] = b % 16 diff --git a/trie/stacktrie.go b/trie/stacktrie.go index 2b7366c3c5..19194263ba 100644 --- a/trie/stacktrie.go +++ b/trie/stacktrie.go @@ -59,6 +59,7 @@ func NewStackTrie(onTrieNode OnTrieNode) *StackTrie { root: stPool.Get().(*stNode), h: newHasher(false), onTrieNode: onTrieNode, + last: make([]byte, 64), kBuf: make([]byte, 64), pBuf: make([]byte, 64), } @@ -83,11 +84,7 @@ func (t *StackTrie) Update(key, value []byte) error { if bytes.Compare(t.last, k) >= 0 { return errors.New("non-ascending key order") } - if t.last == nil { - t.last = append([]byte{}, k...) // allocate key slice - } else { - t.last = append(t.last[:0], k...) // reuse key slice - } + t.last = append(t.last[:0], k...) // reuse key slice t.insert(t.root, k, value, t.pBuf[:0]) return nil } @@ -95,7 +92,7 @@ func (t *StackTrie) Update(key, value []byte) error { // Reset resets the stack trie object to empty state. func (t *StackTrie) Reset() { t.root = stPool.Get().(*stNode) - t.last = nil + t.last = t.last[:0] } // TrieKey returns the internal key representation for the given user key.