From 40789748f74becd9c1406c687d0f18663b61bfcf Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 16 Dec 2019 11:41:19 +0100 Subject: [PATCH] trie/hasher: skip rlp encoding when not needed --- trie/hasher.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/trie/hasher.go b/trie/hasher.go index 54f6a9de2b..a985936491 100644 --- a/trie/hasher.go +++ b/trie/hasher.go @@ -165,20 +165,20 @@ func (h *hasher) store(n node, db *Database, force bool) (node, error) { if _, isHash := n.(hashNode); n == nil || isHash { return n, nil } - // Generate the RLP encoding of the node - h.tmp.Reset() - if err := rlp.Encode(&h.tmp, n); err != nil { - panic("encode error: " + err.Error()) - } - if len(h.tmp) < 32 && !force { - return n, nil // Nodes smaller than 32 bytes are stored inside their parent - } - // Larger nodes are replaced by their hash and stored in the database. + // We might already have the hash hash, _ := n.cache() if hash == nil { + // Generate the RLP encoding of the node + h.tmp.Reset() + if err := rlp.Encode(&h.tmp, n); err != nil { + panic("encode error: " + err.Error()) + } + if len(h.tmp) < 32 && !force { + return n, nil // Nodes smaller than 32 bytes are stored inside their parent + } + // Larger nodes are replaced by their hash and stored in the database. hash = h.makeHashNode(h.tmp) } - if db != nil { // We are pooling the trie nodes into an intermediate memory cache hash := common.BytesToHash(hash)