aboutsummaryrefslogtreecommitdiffstats
path: root/trie/hasher.go
diff options
context:
space:
mode:
authorJeffrey Wilcke <jeffrey@ethereum.org>2016-10-19 19:35:49 +0800
committerGitHub <noreply@github.com>2016-10-19 19:35:49 +0800
commit25ac04a444d82f42138fc06e651c1ef9bac935dc (patch)
tree4b4925de834f243a05c73e661e77bc60287aeb9d /trie/hasher.go
parent8e52c2e754cdb343d0eb880a33251e1ba593d327 (diff)
parent8d56bf5ceb74a7ed45c986450848a89e2df61189 (diff)
downloaddexon-25ac04a444d82f42138fc06e651c1ef9bac935dc.tar.gz
dexon-25ac04a444d82f42138fc06e651c1ef9bac935dc.tar.zst
dexon-25ac04a444d82f42138fc06e651c1ef9bac935dc.zip
Merge pull request #3153 from fjl/trie-unload-fix
trie: improve cache unloading mechanism
Diffstat (limited to 'trie/hasher.go')
-rw-r--r--trie/hasher.go33
1 files changed, 15 insertions, 18 deletions
diff --git a/trie/hasher.go b/trie/hasher.go
index e395e00d7..b6223bf32 100644
--- a/trie/hasher.go
+++ b/trie/hasher.go
@@ -58,7 +58,7 @@ func (h *hasher) hash(n node, db DatabaseWriter, force bool) (node, node, error)
return hash, n, nil
}
if n.canUnload(h.cachegen, h.cachelimit) {
- // Evict the node from cache. All of its subnodes will have a lower or equal
+ // Unload the node from cache. All of its subnodes will have a lower or equal
// cache generation number.
return hash, hash, nil
}
@@ -75,23 +75,20 @@ func (h *hasher) hash(n node, db DatabaseWriter, force bool) (node, node, error)
if err != nil {
return hashNode{}, n, err
}
- // Cache the hash of the ndoe for later reuse.
- if hash, ok := hashed.(hashNode); ok && !force {
- switch cached := cached.(type) {
- case *shortNode:
- cached = cached.copy()
- cached.flags.hash = hash
- if db != nil {
- cached.flags.dirty = false
- }
- return hashed, cached, nil
- case *fullNode:
- cached = cached.copy()
- cached.flags.hash = hash
- if db != nil {
- cached.flags.dirty = false
- }
- return hashed, cached, nil
+ // Cache the hash of the ndoe for later reuse and remove
+ // the dirty flag in commit mode. It's fine to assign these values directly
+ // without copying the node first because hashChildren copies it.
+ cachedHash, _ := hashed.(hashNode)
+ switch cn := cached.(type) {
+ case *shortNode:
+ cn.flags.hash = cachedHash
+ if db != nil {
+ cn.flags.dirty = false
+ }
+ case *fullNode:
+ cn.flags.hash = cachedHash
+ if db != nil {
+ cn.flags.dirty = false
}
}
return hashed, cached, nil