From 3c26ba4d307bf32ac3d17ab6d3f996096a102b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Fri, 23 Sep 2016 08:57:51 +0300 Subject: [PATCH] state: track code dirtyness and only write if changed --- core/state/state_object.go | 10 ++++++---- core/state/statedb.go | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index 5f30177cde..0379af2de2 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -82,9 +82,10 @@ type StateObject struct { // Cache flags. // When an object is marked for deletion it will be delete from the trie // during the "update" phase of the state transition - dirty bool // true if anything has changed - remove bool - deleted bool + dirty bool // true if anything has changed + dirtyCode bool // true if the code was updated + remove bool + deleted bool } // Account is the Ethereum consensus representation of accounts. @@ -238,6 +239,7 @@ func (self *StateObject) Copy(db trie.Database) *StateObject { stateObject.storage = self.storage.Copy() stateObject.remove = self.remove stateObject.dirty = self.dirty + stateObject.dirtyCode = self.dirtyCode stateObject.deleted = self.deleted return stateObject } @@ -270,7 +272,7 @@ func (self *StateObject) Code(db trie.Database) []byte { func (self *StateObject) SetCode(code []byte) { self.code = code self.data.CodeHash = crypto.Keccak256(code) - self.dirty = true + self.dirty, self.dirtyCode = true, true } func (self *StateObject) SetNonce(nonce uint64) { diff --git a/core/state/statedb.go b/core/state/statedb.go index 6d69a9e51e..097e5a1a6f 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -447,10 +447,11 @@ func (s *StateDB) commit(dbw trie.DatabaseWriter) (root common.Hash, err error) delete(s.all, addr) } else if stateObject.dirty { // Write any contract code associated with the state object - if stateObject.code != nil { + if stateObject.code != nil && stateObject.dirtyCode { if err := dbw.Put(stateObject.CodeHash(), stateObject.code); err != nil { return common.Hash{}, err } + stateObject.dirtyCode = false } // Write any storage changes in the state object to its storage trie. if err := stateObject.CommitTrie(s.db, dbw); err != nil {