From 4b915af2c3a0097eac87b06915605f90cc8d14c6 Mon Sep 17 00:00:00 2001 From: CPerezz <37264926+CPerezz@users.noreply.github.com> Date: Tue, 17 Mar 2026 11:42:42 +0100 Subject: [PATCH 1/2] core/state: avoid Bytes() allocation in flatReader hash computations (#34025) ## Summary Replace `addr.Bytes()` and `key.Bytes()` with `addr[:]` and `key[:]` in `flatReader`'s `Account` and `Storage` methods. The former allocates a copy while the latter creates a zero-allocation slice header over the existing backing array. ## Benchmark (AMD EPYC 48-core, 500K entries, screening `--benchtime=1x`) | Metric | Baseline | Slice syntax | Delta | |--------|----------|--------------|-------| | Approve (Mgas/s) | 4.13 | 4.22 | +2.2% | | BalanceOf (Mgas/s) | 168.3 | 190.0 | **+12.9%** | --- core/state/reader.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/state/reader.go b/core/state/reader.go index 49375c467c..fe0ec71f2d 100644 --- a/core/state/reader.go +++ b/core/state/reader.go @@ -98,7 +98,7 @@ func newFlatReader(reader database.StateReader) *flatReader { // // The returned account might be nil if it's not existent. func (r *flatReader) Account(addr common.Address) (*types.StateAccount, error) { - account, err := r.reader.Account(crypto.Keccak256Hash(addr.Bytes())) + account, err := r.reader.Account(crypto.Keccak256Hash(addr[:])) if err != nil { return nil, err } @@ -128,8 +128,8 @@ func (r *flatReader) Account(addr common.Address) (*types.StateAccount, error) { // // The returned storage slot might be empty if it's not existent. func (r *flatReader) Storage(addr common.Address, key common.Hash) (common.Hash, error) { - addrHash := crypto.Keccak256Hash(addr.Bytes()) - slotHash := crypto.Keccak256Hash(key.Bytes()) + addrHash := crypto.Keccak256Hash(addr[:]) + slotHash := crypto.Keccak256Hash(key[:]) ret, err := r.reader.Storage(addrHash, slotHash) if err != nil { return common.Hash{}, err From 519a450c436970a8319f6c7cf383bf99cbc2c55d Mon Sep 17 00:00:00 2001 From: CPerezz <37264926+CPerezz@users.noreply.github.com> Date: Tue, 17 Mar 2026 12:27:29 +0100 Subject: [PATCH 2/2] core/state: skip redundant trie Commit for Verkle in stateObject.commit (#34021) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary **Bug fix.** In Verkle mode, all state objects share a single unified trie (`OpenStorageTrie` returns `self`). During `stateDB.commit()`, the main account trie is committed via `s.trie.Commit(true)`, which calls `CollectNodes` to traverse and serialize the entire tree. However, each dirty account's `obj.commit()` also calls `s.trie.Commit(false)` on the **same trie object**, redundantly traversing and serializing the full tree once per dirty account. With N dirty accounts per block, this causes **N+1 full-tree traversals** instead of 1. On a write-heavy workload (2250 SSTOREs), this produces ~131 GB of allocations per block from duplicate NodeSet creation and serialization. It also causes a latent data race from N+1 goroutines concurrently calling `CollectNodes` on shared `InternalNode` objects. This commit adds an `IsVerkle()` early return in `stateObject.commit()` to skip the redundant `trie.Commit()` call. ## Benchmark (AMD EPYC 48-core, 500K entries, `--benchtime=10s --count=3`) | Metric | Baseline | Fixed | Delta | |--------|----------|-------|-------| | Approve (Mgas/s) | 4.16 ± 0.37 | **220.2 ± 10.1** | **+5190%** | | BalanceOf (Mgas/s) | 966.2 ± 8.1 | 971.0 ± 3.0 | +0.5% | | Allocs/op (approve) | 136.4M | 792K | **-99.4%** | Resolves the TODO in statedb.go about the account trie commit being "very heavy" and "something's wonky". --------- Co-authored-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> --- core/state/state_object.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/state/state_object.go b/core/state/state_object.go index dd30bb64a5..ec0c511737 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -474,6 +474,14 @@ func (s *stateObject) commit() (*accountUpdate, *trienode.NodeSet, error) { s.origin = s.data.Copy() return op, nil, nil } + // In Verkle/binary trie mode, all state objects share one unified trie. + // The main account trie commit in stateDB.commit() already calls + // CollectNodes on this trie, so calling Commit here again would + // redundantly traverse and serialize the entire tree per dirty account. + if s.db.GetTrie().IsVerkle() { + s.origin = s.data.Copy() + return op, nil, nil + } root, nodes := s.trie.Commit(false) s.data.Root = root s.origin = s.data.Copy()