From adb545b20cca92b163d730686b4991b041d6b099 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Fri, 1 May 2026 15:31:22 +0200 Subject: [PATCH] core/state: colocate StateCounts/ReadDurations with StateDB Moves the snapshot DTOs into statedb.go directly above SnapshotCounts and SnapshotReads, so the types and their sole constructors live in the same file. Avoids a single-purpose state_counts.go. --- core/state/state_counts.go | 69 -------------------------------------- core/state/statedb.go | 50 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 69 deletions(-) delete mode 100644 core/state/state_counts.go diff --git a/core/state/state_counts.go b/core/state/state_counts.go deleted file mode 100644 index 457dcbf78e..0000000000 --- a/core/state/state_counts.go +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2026 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package state - -import "time" - -// ReadDurations groups the {Account, Storage, Code} state-read times that are -// aggregated across pre-tx, per-tx and post-tx statedbs in the BAL parallel -// path. Sum-of-CPU-time, not wall-clock. -type ReadDurations struct { - Account time.Duration - Storage time.Duration - Code time.Duration -} - -// Add merges other into r. -func (r *ReadDurations) Add(other ReadDurations) { - r.Account += other.Account - r.Storage += other.Storage - r.Code += other.Code -} - -// StateCounts holds count-only statistics gathered during a block's state -// transition. Plain-int snapshot type, safe to copy through channels. -// Atomic counters on StateDB are converted at the snapshot boundary in -// SnapshotCounts. Read durations live in ReadDurations (separate type). -type StateCounts struct { - AccountLoaded int // accounts retrieved from the database during the state transition - AccountUpdated int // accounts updated during the state transition - AccountDeleted int // accounts deleted during the state transition - StorageLoaded int // storage slots retrieved from the database during the state transition - StorageUpdated int64 // storage slots updated (snapshotted from atomic on StateDB) - StorageDeleted int64 // storage slots deleted (snapshotted from atomic on StateDB) - CodeLoaded int // contract code reads - CodeLoadBytes int // total bytes of resolved code - CodeUpdated int // code writes (CREATE/CREATE2/EIP-7702) - CodeUpdateBytes int // total bytes of persisted code written -} - -// Add merges other into c. Plain integer addition — no atomics here, since -// StateCounts is the snapshot type. The receiver is the only mutated party; -// other is taken by value (the struct is small and value semantics matches -// the snapshot thesis stated above). -func (c *StateCounts) Add(other StateCounts) { - c.AccountLoaded += other.AccountLoaded - c.AccountUpdated += other.AccountUpdated - c.AccountDeleted += other.AccountDeleted - c.StorageLoaded += other.StorageLoaded - c.StorageUpdated += other.StorageUpdated - c.StorageDeleted += other.StorageDeleted - c.CodeLoaded += other.CodeLoaded - c.CodeLoadBytes += other.CodeLoadBytes - c.CodeUpdated += other.CodeUpdated - c.CodeUpdateBytes += other.CodeUpdateBytes -} diff --git a/core/state/statedb.go b/core/state/statedb.go index 6719182e13..04f90d8cf0 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -223,6 +223,56 @@ func (s *StateDB) WithReader(reader Reader) *StateDB { return cpy } +// ReadDurations groups the {Account, Storage, Code} state-read times that are +// aggregated across pre-tx, per-tx and post-tx statedbs in the BAL parallel +// path. Sum-of-CPU-time, not wall-clock. +type ReadDurations struct { + Account time.Duration + Storage time.Duration + Code time.Duration +} + +// Add merges other into r. +func (r *ReadDurations) Add(other ReadDurations) { + r.Account += other.Account + r.Storage += other.Storage + r.Code += other.Code +} + +// StateCounts holds count-only statistics gathered during a block's state +// transition. Plain-int snapshot type, safe to copy through channels. +// Atomic counters on StateDB are converted at the snapshot boundary in +// SnapshotCounts. Read durations live in ReadDurations (separate type). +type StateCounts struct { + AccountLoaded int // accounts retrieved from the database during the state transition + AccountUpdated int // accounts updated during the state transition + AccountDeleted int // accounts deleted during the state transition + StorageLoaded int // storage slots retrieved from the database during the state transition + StorageUpdated int64 // storage slots updated (snapshotted from atomic on StateDB) + StorageDeleted int64 // storage slots deleted (snapshotted from atomic on StateDB) + CodeLoaded int // contract code reads + CodeLoadBytes int // total bytes of resolved code + CodeUpdated int // code writes (CREATE/CREATE2/EIP-7702) + CodeUpdateBytes int // total bytes of persisted code written +} + +// Add merges other into c. Plain integer addition — no atomics here, since +// StateCounts is the snapshot type. The receiver is the only mutated party; +// other is taken by value (the struct is small and value semantics matches +// the snapshot thesis stated above). +func (c *StateCounts) Add(other StateCounts) { + c.AccountLoaded += other.AccountLoaded + c.AccountUpdated += other.AccountUpdated + c.AccountDeleted += other.AccountDeleted + c.StorageLoaded += other.StorageLoaded + c.StorageUpdated += other.StorageUpdated + c.StorageDeleted += other.StorageDeleted + c.CodeLoaded += other.CodeLoaded + c.CodeLoadBytes += other.CodeLoadBytes + c.CodeUpdated += other.CodeUpdated + c.CodeUpdateBytes += other.CodeUpdateBytes +} + // SnapshotCounts returns a value-copy of the state-mutation counters as a // plain-int StateCounts. Atomic fields are read via Load(); the result is // safe to copy, pass through channels, and aggregate via StateCounts.Add.