From c5f1e2fbcdc4f740956bcd5a4e97104ef81a6f74 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 8 Feb 2022 18:15:05 +0300 Subject: [PATCH] mpt: don't allocate for every key Microoptimization. --- pkg/core/mpt/trie.go | 9 +++++---- pkg/core/mpt/trie_test.go | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/core/mpt/trie.go b/pkg/core/mpt/trie.go index fef9e682f..f1d2deebc 100644 --- a/pkg/core/mpt/trie.go +++ b/pkg/core/mpt/trie.go @@ -407,18 +407,20 @@ func makeStorageKey(mptKey util.Uint256) []byte { // new node to storage. Normally, flush should be called with every StateRoot persist, i.e. // after every block. func (t *Trie) Flush(index uint32) { + key := makeStorageKey(util.Uint256{}) for h, node := range t.refcount { if node.refcount != 0 { + copy(key[1:], h[:]) if node.bytes == nil { panic("item not in trie") } if t.mode.RC() { - node.initial = t.updateRefCount(h, index) + node.initial = t.updateRefCount(h, key, index) if node.initial == 0 { delete(t.refcount, h) } } else if node.refcount > 0 { - _ = t.Store.Put(makeStorageKey(h), node.bytes) + _ = t.Store.Put(key, node.bytes) } node.refcount = 0 } else { @@ -440,12 +442,11 @@ func getFromStore(key []byte, mode TrieMode, store *storage.MemCachedStore) ([]b } // updateRefCount should be called only when refcounting is enabled. -func (t *Trie) updateRefCount(h util.Uint256, index uint32) int32 { +func (t *Trie) updateRefCount(h util.Uint256, key []byte, index uint32) int32 { if !t.mode.RC() { panic("`updateRefCount` is called, but GC is disabled") } var data []byte - key := makeStorageKey(h) node := t.refcount[h] cnt := node.initial if cnt == 0 { diff --git a/pkg/core/mpt/trie_test.go b/pkg/core/mpt/trie_test.go index e7eda2d0d..97c5ef4e4 100644 --- a/pkg/core/mpt/trie_test.go +++ b/pkg/core/mpt/trie_test.go @@ -249,7 +249,7 @@ func (tr *Trie) putToStore(n Node) { bytes: n.Bytes(), refcount: 1, } - tr.updateRefCount(n.Hash(), 0) + tr.updateRefCount(n.Hash(), makeStorageKey(n.Hash()), 0) } else { _ = tr.Store.Put(makeStorageKey(n.Hash()), n.Bytes()) }