From 8f4759db9d15df6488a93bf1423f784418f5ad51 Mon Sep 17 00:00:00 2001 From: Aaron Chen Date: Wed, 17 Apr 2024 17:08:24 +0800 Subject: [PATCH 1/3] core/state: remove unnecessary preallocation in StateDB.Copy Both stateObjectsDestruct and preimages fields are assigned by the value returned by maps.Clone. The preallocation is a waste. --- core/state/statedb.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index d3d383389c23..6f76d082d185 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -703,11 +703,11 @@ func (s *StateDB) Copy() *StateDB { stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)), stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)), stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)), - stateObjectsDestruct: make(map[common.Address]*types.StateAccount, len(s.stateObjectsDestruct)), + stateObjectsDestruct: make(map[common.Address]*types.StateAccount), refund: s.refund, logs: make(map[common.Hash][]*types.Log, len(s.logs)), logSize: s.logSize, - preimages: make(map[common.Hash][]byte, len(s.preimages)), + preimages: make(map[common.Hash][]byte), journal: newJournal(), hasher: crypto.NewKeccakState(), From 204810354de975d39481c203e407caf07bb6ba54 Mon Sep 17 00:00:00 2001 From: Aaron Chen Date: Wed, 17 Apr 2024 17:36:13 +0800 Subject: [PATCH 2/3] core/state: remove unnecessary preallocation in StateDB.Copy --- core/state/statedb.go | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 6f76d082d185..28d7fa5c508c 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -696,18 +696,18 @@ func (s *StateDB) Copy() *StateDB { db: s.db, trie: s.db.CopyTrie(s.trie), originalRoot: s.originalRoot, - accounts: make(map[common.Hash][]byte), - storages: make(map[common.Hash]map[common.Hash][]byte), - accountsOrigin: make(map[common.Address][]byte), - storagesOrigin: make(map[common.Address]map[common.Hash][]byte), + accounts: copySet(s.accounts), + storages: copy2DSet(s.storages), + accountsOrigin: copySet(state.accountsOrigin), + storagesOrigin: copy2DSet(state.storagesOrigin), stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)), stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)), stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)), - stateObjectsDestruct: make(map[common.Address]*types.StateAccount), + stateObjectsDestruct: maps.Clone(s.stateObjectsDestruct), refund: s.refund, logs: make(map[common.Hash][]*types.Log, len(s.logs)), logSize: s.logSize, - preimages: make(map[common.Hash][]byte), + preimages: maps.Clone(s.preimages), journal: newJournal(), hasher: crypto.NewKeccakState(), @@ -750,15 +750,6 @@ func (s *StateDB) Copy() *StateDB { } state.stateObjectsDirty[addr] = struct{}{} } - // Deep copy the destruction markers. - state.stateObjectsDestruct = maps.Clone(s.stateObjectsDestruct) - - // Deep copy the state changes made in the scope of block - // along with their original values. - state.accounts = copySet(s.accounts) - state.storages = copy2DSet(s.storages) - state.accountsOrigin = copySet(state.accountsOrigin) - state.storagesOrigin = copy2DSet(state.storagesOrigin) // Deep copy the logs occurred in the scope of block for hash, logs := range s.logs { @@ -769,8 +760,7 @@ func (s *StateDB) Copy() *StateDB { } state.logs[hash] = cpy } - // Deep copy the preimages occurred in the scope of block - state.preimages = maps.Clone(s.preimages) + // Do we need to copy the access list and transient storage? // In practice: No. At the start of a transaction, these two lists are empty. // In practice, we only ever copy state _between_ transactions/blocks, never From 02ffe7e09645ccbd2be24c095b2e9dfd599a216e Mon Sep 17 00:00:00 2001 From: Aaron Chen Date: Wed, 17 Apr 2024 18:14:51 +0800 Subject: [PATCH 3/3] copy s.accountsOrigin rather than state.accountsOrigin --- core/state/statedb.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 28d7fa5c508c..ab152dd18d66 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -698,8 +698,8 @@ func (s *StateDB) Copy() *StateDB { originalRoot: s.originalRoot, accounts: copySet(s.accounts), storages: copy2DSet(s.storages), - accountsOrigin: copySet(state.accountsOrigin), - storagesOrigin: copy2DSet(state.storagesOrigin), + accountsOrigin: copySet(s.accountsOrigin), + storagesOrigin: copy2DSet(s.storagesOrigin), stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)), stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)), stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)),