diff --git a/core/state/statedb.go b/core/state/statedb.go index cc2154cfb9..efb09a08a0 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1464,11 +1464,3 @@ func (s *StateDB) Witness() *stateless.Witness { func (s *StateDB) AccessEvents() *AccessEvents { return s.accessEvents } - -func (s *StateDB) BeginTrieDepthWindow() { - trie.StateDepthMetricsStartBlock() -} - -func (s *StateDB) EndTrieDepthWindow() { - trie.StateDepthMetricsEndBlock() -} diff --git a/core/state_processor.go b/core/state_processor.go index ca0d27ee39..ee98326467 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -64,9 +64,6 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg allLogs []*types.Log gp = new(GasPool).AddGas(block.GasLimit()) ) - // Start the trie depth metrics window - statedb.BeginTrieDepthWindow() - defer statedb.EndTrieDepthWindow() // Mutate the block and state according to any hard-fork specs if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 { diff --git a/trie/trie.go b/trie/trie.go index cc4ae30ba6..c5b0377c12 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -21,7 +21,6 @@ import ( "bytes" "errors" "fmt" - "slices" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -376,10 +375,6 @@ func (t *Trie) update(key, value []byte) error { func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) { if len(key) == 0 { - if t.owner == (common.Hash{}) { - stateDepthAggregator.record(int64(len(prefix))) - } - if v, ok := n.(valueNode); ok { return !bytes.Equal(v, value.(valueNode)), value, nil } @@ -522,7 +517,10 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) { // always creates a new slice) instead of append to // avoid modifying n.Key since it might be shared with // other nodes. - return true, &shortNode{slices.Concat(n.Key, child.Key), child.Val, t.newFlag()}, nil + if t.owner == (common.Hash{}) { + stateDepthAggregator.record(int64(len(prefix) + len(key))) + } + return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil default: return true, &shortNode{n.Key, child, t.newFlag()}, nil } @@ -579,7 +577,10 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) { // Replace the entire full node with the short node. // Mark the original short node as deleted since the // value is embedded into the parent now. - t.opTracer.onDelete(append(prefix, byte(pos))) + t.tracer.onDelete(append(prefix, byte(pos))) + if t.owner == (common.Hash{}) { + stateDepthAggregator.record(int64(len(prefix) + 1)) + } k := append([]byte{byte(pos)}, cnode.Key...) return true, &shortNode{k, cnode.Val, t.newFlag()}, nil diff --git a/trie/trie_metrics.go b/trie/trie_metrics.go deleted file mode 100644 index 1f38b73243..0000000000 --- a/trie/trie_metrics.go +++ /dev/null @@ -1,56 +0,0 @@ -package trie - -import ( - "sync" - - "github.com/ethereum/go-ethereum/metrics" -) - -var ( - avgAccessDepthInBlock = metrics.NewRegisteredGauge("trie/access/depth/avg", nil) - minAccessDepthInBlock = metrics.NewRegisteredGauge("trie/access/depth/min", nil) - stateDepthAggregator = &depthAggregator{} -) - -// depthAggregator aggregates trie access depth metrics for a block. -type depthAggregator struct { - mu sync.Mutex - sum, cnt int64 - min int64 -} - -// start initializes the aggregator for a new block. -func (d *depthAggregator) start() { - d.mu.Lock() - d.sum, d.cnt, d.min = 0, 0, -1 - d.mu.Unlock() -} - -// record records the access depth for a trie operation. -func (d *depthAggregator) record(depth int64) { - d.mu.Lock() - v := depth - d.sum += v - d.cnt++ - if d.min < 0 || v < d.min { - d.min = v - } - d.mu.Unlock() -} - -// end finalizes the metrics for the current block and updates the registered metrics. -func (d *depthAggregator) end() { - d.mu.Lock() - sum, cnt, min := d.sum, d.cnt, d.min - d.mu.Unlock() - if cnt > 0 { - avgAccessDepthInBlock.Update(sum / cnt) - minAccessDepthInBlock.Update(min) - } -} - -// StateDepthMetricsStartBlock initializes the depth aggregator for a new block. -func StateDepthMetricsStartBlock() { stateDepthAggregator.start() } - -// StateDepthMetricsEndBlock finalizes the depth metrics for the current block. -func StateDepthMetricsEndBlock() { stateDepthAggregator.end() }