@@ -157,11 +157,20 @@ func (s *stateObject) touch() {
157157
158158func (s * stateObject ) getTrie (db Database ) Trie {
159159 if s .trie == nil {
160- var err error
161- s .trie , err = db .OpenStorageTrie (s .addrHash , s .data .Root )
162- if err != nil {
163- s .trie , _ = db .OpenStorageTrie (s .addrHash , common.Hash {})
164- s .setError (fmt .Errorf ("can't create storage trie: %v" , err ))
160+ // Try fetching from prefetcher first
161+ // We don't prefetch empty tries
162+ if s .data .Root != emptyRoot && s .db .prefetcher != nil {
163+ // When the miner is creating the pending state, there is no
164+ // prefetcher
165+ s .trie = s .db .prefetcher .GetTrie (s .data .Root )
166+ }
167+ if s .trie == nil {
168+ var err error
169+ s .trie , err = db .OpenStorageTrie (s .addrHash , s .data .Root )
170+ if err != nil {
171+ s .trie , _ = db .OpenStorageTrie (s .addrHash , common.Hash {})
172+ s .setError (fmt .Errorf ("can't create storage trie: %v" , err ))
173+ }
165174 }
166175 }
167176 return s .trie
@@ -197,12 +206,24 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
197206 }
198207 // If no live objects are available, attempt to use snapshots
199208 var (
200- enc []byte
201- err error
209+ enc []byte
210+ err error
211+ meter * time.Duration
202212 )
213+ readStart := time .Now ()
214+ if metrics .EnabledExpensive {
215+ // If the snap is 'under construction', the first lookup may fail. If that
216+ // happens, we don't want to double-count the time elapsed. Thus this
217+ // dance with the metering.
218+ defer func () {
219+ if meter != nil {
220+ * meter += time .Since (readStart )
221+ }
222+ }()
223+ }
203224 if s .db .snap != nil {
204225 if metrics .EnabledExpensive {
205- defer func ( start time. Time ) { s .db .SnapshotStorageReads += time . Since ( start ) }( time . Now ())
226+ meter = & s .db .SnapshotStorageReads
206227 }
207228 // If the object was destructed in *this* block (and potentially resurrected),
208229 // the storage has been cleared out, and we should *not* consult the previous
@@ -217,8 +238,14 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
217238 }
218239 // If snapshot unavailable or reading from it failed, load from the database
219240 if s .db .snap == nil || err != nil {
241+ if meter != nil {
242+ // If we already spent time checking the snapshot, account for it
243+ // and reset the readStart
244+ * meter += time .Since (readStart )
245+ readStart = time .Now ()
246+ }
220247 if metrics .EnabledExpensive {
221- defer func ( start time. Time ) { s .db .StorageReads += time . Since ( start ) }( time . Now ())
248+ meter = & s .db .StorageReads
222249 }
223250 if enc , err = s .getTrie (db ).TryGet (key .Bytes ()); err != nil {
224251 s .setError (err )
@@ -283,8 +310,13 @@ func (s *stateObject) setState(key, value common.Hash) {
283310// finalise moves all dirty storage slots into the pending area to be hashed or
284311// committed later. It is invoked at the end of every transaction.
285312func (s * stateObject ) finalise () {
313+ trieChanges := make ([]common.Hash , 0 , len (s .dirtyStorage ))
286314 for key , value := range s .dirtyStorage {
287315 s .pendingStorage [key ] = value
316+ trieChanges = append (trieChanges , key )
317+ }
318+ if len (trieChanges ) > 0 && s .db .prefetcher != nil && s .data .Root != emptyRoot {
319+ s .db .prefetcher .PrefetchStorage (s .data .Root , trieChanges )
288320 }
289321 if len (s .dirtyStorage ) > 0 {
290322 s .dirtyStorage = make (Storage )
@@ -303,18 +335,11 @@ func (s *stateObject) updateTrie(db Database) Trie {
303335 if metrics .EnabledExpensive {
304336 defer func (start time.Time ) { s .db .StorageUpdates += time .Since (start ) }(time .Now ())
305337 }
306- // Retrieve the snapshot storage map for the object
338+ // The snapshot storage map for the object
307339 var storage map [common.Hash ][]byte
308- if s .db .snap != nil {
309- // Retrieve the old storage map, if available, create a new one otherwise
310- storage = s .db .snapStorage [s .addrHash ]
311- if storage == nil {
312- storage = make (map [common.Hash ][]byte )
313- s .db .snapStorage [s .addrHash ] = storage
314- }
315- }
316340 // Insert all the pending updates into the trie
317341 tr := s .getTrie (db )
342+ hasher := s .db .hasher
318343 for key , value := range s .pendingStorage {
319344 // Skip noop changes, persist actual changes
320345 if value == s .originStorage [key ] {
@@ -331,8 +356,15 @@ func (s *stateObject) updateTrie(db Database) Trie {
331356 s .setError (tr .TryUpdate (key [:], v ))
332357 }
333358 // If state snapshotting is active, cache the data til commit
334- if storage != nil {
335- storage [crypto .Keccak256Hash (key [:])] = v // v will be nil if value is 0x00
359+ if s .db .snap != nil {
360+ if storage == nil {
361+ // Retrieve the old storage map, if available, create a new one otherwise
362+ if storage = s .db .snapStorage [s .addrHash ]; storage == nil {
363+ storage = make (map [common.Hash ][]byte )
364+ s .db .snapStorage [s .addrHash ] = storage
365+ }
366+ }
367+ storage [crypto .HashData (hasher , key [:])] = v // v will be nil if value is 0x00
336368 }
337369 }
338370 if len (s .pendingStorage ) > 0 {
0 commit comments