From 2b53877dff71f592d3f7ee01125403b14bd45565 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 3 Jun 2020 00:02:45 +0300 Subject: [PATCH] mpt: don't flush nodes already present in the DB It's just a waste of time. Signed-off-by: Evgenii Stratonikov --- pkg/core/mpt/base.go | 15 +++++++++++++++ pkg/core/mpt/trie.go | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/pkg/core/mpt/base.go b/pkg/core/mpt/base.go index f8bce0b34..9f10cc333 100644 --- a/pkg/core/mpt/base.go +++ b/pkg/core/mpt/base.go @@ -14,6 +14,8 @@ type BaseNode struct { bytes []byte hashValid bool bytesValid bool + + isFlushed bool } // BaseNodeIface abstracts away basic Node functions. @@ -21,6 +23,8 @@ type BaseNodeIface interface { Hash() util.Uint256 Type() NodeType Bytes() []byte + IsFlushed() bool + SetFlushed() } // getHash returns a hash of this BaseNode. @@ -60,6 +64,17 @@ func (b *BaseNode) updateBytes(n Node) { func (b *BaseNode) invalidateCache() { b.bytesValid = false b.hashValid = false + b.isFlushed = false +} + +// IsFlushed checks for node flush status. +func (b *BaseNode) IsFlushed() bool { + return b.isFlushed +} + +// SetFlushed sets 'flushed' flag to true for this node. +func (b *BaseNode) SetFlushed() { + b.isFlushed = true } // encodeNodeWithType encodes node together with it's type. diff --git a/pkg/core/mpt/trie.go b/pkg/core/mpt/trie.go index d9b6f7b0d..3c38424c0 100644 --- a/pkg/core/mpt/trie.go +++ b/pkg/core/mpt/trie.go @@ -318,6 +318,9 @@ func (t *Trie) Flush() { } func (t *Trie) flush(node Node) { + if node.IsFlushed() { + return + } switch n := node.(type) { case *BranchNode: for i := range n.Children { @@ -336,6 +339,7 @@ func (t *Trie) putToStore(n Node) { panic("can't put hash node in trie") } _ = t.Store.Put(makeStorageKey(n.Hash().BytesBE()), n.Bytes()) // put in MemCached returns no errors + n.SetFlushed() } func (t *Trie) getFromStore(h util.Uint256) (Node, error) {