a small improvement to reduce gc about the latest field

This commit is contained in:
maskpp 2025-09-07 15:32:27 +08:00
parent 8ce2047348
commit a7f2803f8e
2 changed files with 3 additions and 7 deletions

View file

@ -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

View file

@ -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.