core/state: only calculate the addressHash when needed

This commit is contained in:
MariusVanDerWijden 2025-12-05 17:02:26 +01:00
parent 35082b286f
commit 3712b19147
2 changed files with 23 additions and 16 deletions

View file

@ -47,11 +47,11 @@ func (s Storage) Copy() Storage {
// - Account values as well as storages can be accessed and modified through the object. // - Account values as well as storages can be accessed and modified through the object.
// - Finally, call commit to return the changes of storage trie and update account data. // - Finally, call commit to return the changes of storage trie and update account data.
type stateObject struct { type stateObject struct {
db *StateDB db *StateDB
address common.Address // address of ethereum account address common.Address // address of ethereum account
addrHash common.Hash // hash of ethereum address of the account addressHash *common.Hash // hash of ethereum address of the account
origin *types.StateAccount // Account original data without any change applied, nil means it was not existent origin *types.StateAccount // Account original data without any change applied, nil means it was not existent
data types.StateAccount // Account data with all mutations applied in the scope of block data types.StateAccount // Account data with all mutations applied in the scope of block
// Write caches. // Write caches.
trie Trie // storage trie, which becomes non-nil on first access trie Trie // storage trie, which becomes non-nil on first access
@ -101,7 +101,6 @@ func newObject(db *StateDB, address common.Address, acct *types.StateAccount) *s
return &stateObject{ return &stateObject{
db: db, db: db,
address: address, address: address,
addrHash: crypto.Keccak256Hash(address[:]),
origin: origin, origin: origin,
data: *acct, data: *acct,
originStorage: make(Storage), originStorage: make(Storage),
@ -111,6 +110,14 @@ func newObject(db *StateDB, address common.Address, acct *types.StateAccount) *s
} }
} }
func (s *stateObject) addrHash() common.Hash {
if s.addressHash == nil {
h := crypto.Keccak256Hash(s.address[:])
s.addressHash = &h
}
return *s.addressHash
}
func (s *stateObject) markSelfdestructed() { func (s *stateObject) markSelfdestructed() {
s.selfDestructed = true s.selfDestructed = true
} }
@ -150,7 +157,7 @@ func (s *stateObject) getPrefetchedTrie() Trie {
return nil return nil
} }
// Attempt to retrieve the trie from the prefetcher // Attempt to retrieve the trie from the prefetcher
return s.db.prefetcher.trie(s.addrHash, s.data.Root) return s.db.prefetcher.trie(s.addrHash(), s.data.Root)
} }
// GetState retrieves a value associated with the given storage key. // GetState retrieves a value associated with the given storage key.
@ -202,7 +209,7 @@ func (s *stateObject) GetCommittedState(key common.Hash) common.Hash {
// Schedule the resolved storage slots for prefetching if it's enabled. // Schedule the resolved storage slots for prefetching if it's enabled.
if s.db.prefetcher != nil && s.data.Root != types.EmptyRootHash { if s.db.prefetcher != nil && s.data.Root != types.EmptyRootHash {
if err = s.db.prefetcher.prefetch(s.addrHash, s.origin.Root, s.address, nil, []common.Hash{key}, true); err != nil { if err = s.db.prefetcher.prefetch(s.addrHash(), s.origin.Root, s.address, nil, []common.Hash{key}, true); err != nil {
log.Error("Failed to prefetch storage slot", "addr", s.address, "key", key, "err", err) log.Error("Failed to prefetch storage slot", "addr", s.address, "key", key, "err", err)
} }
} }
@ -263,7 +270,7 @@ func (s *stateObject) finalise() {
s.pendingStorage[key] = value s.pendingStorage[key] = value
} }
if s.db.prefetcher != nil && len(slotsToPrefetch) > 0 && s.data.Root != types.EmptyRootHash { if s.db.prefetcher != nil && len(slotsToPrefetch) > 0 && s.data.Root != types.EmptyRootHash {
if err := s.db.prefetcher.prefetch(s.addrHash, s.data.Root, s.address, nil, slotsToPrefetch, false); err != nil { if err := s.db.prefetcher.prefetch(s.addrHash(), s.data.Root, s.address, nil, slotsToPrefetch, false); err != nil {
log.Error("Failed to prefetch slots", "addr", s.address, "slots", len(slotsToPrefetch), "err", err) log.Error("Failed to prefetch slots", "addr", s.address, "slots", len(slotsToPrefetch), "err", err)
} }
} }
@ -358,7 +365,7 @@ func (s *stateObject) updateTrie() (Trie, error) {
s.db.StorageDeleted.Add(1) s.db.StorageDeleted.Add(1)
} }
if s.db.prefetcher != nil { if s.db.prefetcher != nil {
s.db.prefetcher.used(s.addrHash, s.data.Root, nil, used) s.db.prefetcher.used(s.addrHash(), s.data.Root, nil, used)
} }
s.uncommittedStorage = make(Storage) // empties the commit markers s.uncommittedStorage = make(Storage) // empties the commit markers
return tr, nil return tr, nil
@ -484,7 +491,7 @@ func (s *stateObject) deepCopy(db *StateDB) *stateObject {
obj := &stateObject{ obj := &stateObject{
db: db, db: db,
address: s.address, address: s.address,
addrHash: s.addrHash, addressHash: nil,
origin: s.origin, origin: s.origin,
data: s.data, data: s.data,
code: s.code, code: s.code,

View file

@ -867,13 +867,13 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
witness := trie.Witness() witness := trie.Witness()
s.witness.AddState(witness) s.witness.AddState(witness)
if s.witnessStats != nil { if s.witnessStats != nil {
s.witnessStats.Add(witness, obj.addrHash) s.witnessStats.Add(witness, obj.addrHash())
} }
} else if obj.trie != nil { } else if obj.trie != nil {
witness := obj.trie.Witness() witness := obj.trie.Witness()
s.witness.AddState(witness) s.witness.AddState(witness)
if s.witnessStats != nil { if s.witnessStats != nil {
s.witnessStats.Add(witness, obj.addrHash) s.witnessStats.Add(witness, obj.addrHash())
} }
} }
} }
@ -891,13 +891,13 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
witness := trie.Witness() witness := trie.Witness()
s.witness.AddState(witness) s.witness.AddState(witness)
if s.witnessStats != nil { if s.witnessStats != nil {
s.witnessStats.Add(witness, obj.addrHash) s.witnessStats.Add(witness, obj.addrHash())
} }
} else if obj.trie != nil { } else if obj.trie != nil {
witness := obj.trie.Witness() witness := obj.trie.Witness()
s.witness.AddState(witness) s.witness.AddState(witness)
if s.witnessStats != nil { if s.witnessStats != nil {
s.witnessStats.Add(witness, obj.addrHash) s.witnessStats.Add(witness, obj.addrHash())
} }
} }
} }
@ -1277,7 +1277,7 @@ func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool, blockNum
return err return err
} }
lock.Lock() lock.Lock()
updates[obj.addrHash] = update updates[obj.addrHash()] = update
s.StorageCommits = time.Since(start) // overwrite with the longest storage commit runtime s.StorageCommits = time.Since(start) // overwrite with the longest storage commit runtime
lock.Unlock() lock.Unlock()
return nil return nil