mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 13:46:43 +00:00
reduce memory and cpu usage
This commit is contained in:
parent
f86870f5da
commit
788c6dc868
2 changed files with 26 additions and 7 deletions
|
|
@ -786,6 +786,18 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
|
||||||
// It is called in between transactions to get the root hash that
|
// It is called in between transactions to get the root hash that
|
||||||
// goes into transaction receipts.
|
// goes into transaction receipts.
|
||||||
func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
|
func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
|
||||||
|
return s.intermediateRoot(deleteEmptyObjects, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntermediateRootPrefetch triggers trie loading for cache warming without
|
||||||
|
// computing the actual root hash, optimized for prefetching.
|
||||||
|
func (s *StateDB) IntermediateRootPrefetch(deleteEmptyObjects bool) {
|
||||||
|
s.intermediateRoot(deleteEmptyObjects, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// intermediateRoot is the internal implementation that supports both normal
|
||||||
|
// and prefetch modes to reduce memory allocations in prefetching scenarios.
|
||||||
|
func (s *StateDB) intermediateRoot(deleteEmptyObjects bool, prefetchMode bool) common.Hash {
|
||||||
// Finalise all the dirty storage states and write them into the tries
|
// Finalise all the dirty storage states and write them into the tries
|
||||||
s.Finalise(deleteEmptyObjects)
|
s.Finalise(deleteEmptyObjects)
|
||||||
|
|
||||||
|
|
@ -835,6 +847,10 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
|
||||||
workers.Go(func() error {
|
workers.Go(func() error {
|
||||||
if s.db.TrieDB().IsVerkle() {
|
if s.db.TrieDB().IsVerkle() {
|
||||||
obj.updateTrie()
|
obj.updateTrie()
|
||||||
|
} else if prefetchMode {
|
||||||
|
// In prefetch mode, flushes cached storage mutations to trie
|
||||||
|
// for cache warming without computing the expensive root hash
|
||||||
|
obj.updateTrie()
|
||||||
} else {
|
} else {
|
||||||
obj.updateRoot()
|
obj.updateRoot()
|
||||||
|
|
||||||
|
|
@ -935,6 +951,13 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
|
||||||
if s.prefetcher != nil {
|
if s.prefetcher != nil {
|
||||||
s.prefetcher.used(common.Hash{}, s.originalRoot, usedAddrs, nil)
|
s.prefetcher.used(common.Hash{}, s.originalRoot, usedAddrs, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// In prefetch mode, skip expensive hash computation since we only want
|
||||||
|
// the side effects of trie loading for cache warming
|
||||||
|
if prefetchMode {
|
||||||
|
return common.Hash{}
|
||||||
|
}
|
||||||
|
|
||||||
// Track the amount of time wasted on hashing the account trie
|
// Track the amount of time wasted on hashing the account trie
|
||||||
defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now())
|
defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now())
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -111,12 +111,9 @@ func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, c
|
||||||
fails.Add(1)
|
fails.Add(1)
|
||||||
return nil // Ugh, something went horribly wrong, bail out
|
return nil // Ugh, something went horribly wrong, bail out
|
||||||
}
|
}
|
||||||
// Pre-load trie nodes for the intermediate root.
|
// Pre-load trie nodes for cache warming without computing the actual
|
||||||
//
|
// intermediate root hash, reducing memory allocations and cpu cost from hashing.
|
||||||
// This operation incurs significant memory allocations due to
|
stateCpy.IntermediateRootPrefetch(true)
|
||||||
// trie hashing and node decoding. TODO(rjl493456442): investigate
|
|
||||||
// ways to mitigate this overhead.
|
|
||||||
stateCpy.IntermediateRoot(true)
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -124,5 +121,4 @@ func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, c
|
||||||
|
|
||||||
blockPrefetchTxsValidMeter.Mark(int64(len(block.Transactions())) - fails.Load())
|
blockPrefetchTxsValidMeter.Mark(int64(len(block.Transactions())) - fails.Load())
|
||||||
blockPrefetchTxsInvalidMeter.Mark(fails.Load())
|
blockPrefetchTxsInvalidMeter.Mark(fails.Load())
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue