From 6f3e3de9c05572f3158cc1a9956de89e4554a254 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 15 Dec 2025 16:20:34 -0800 Subject: [PATCH] perf experiment: fully concurrent origin storage loading --- core/state/bal_state_transition.go | 63 +++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/core/state/bal_state_transition.go b/core/state/bal_state_transition.go index 91e08ac38b..9f20e84462 100644 --- a/core/state/bal_state_transition.go +++ b/core/state/bal_state_transition.go @@ -358,6 +358,54 @@ func (s *BALStateTransition) CommitWithUpdate(block uint64, deleteEmptyObjects b return root, ret, nil } +func (s *BALStateTransition) loadOriginStorages() { + lastIdx := len(s.accessList.block.Transactions()) + 1 + + type originStorage struct { + address common.Address + key common.Hash + value common.Hash + } + + originStoragesCh := make(chan *originStorage) + var pendingStorageCount int + + for _, addr := range s.accessList.ModifiedAccounts() { + diff := s.accessList.readAccountDiff(addr, lastIdx) + pendingStorageCount += len(diff.StorageWrites) + s.originStorages[addr] = make(map[common.Hash]common.Hash) + for key := range diff.StorageWrites { + storageKey := key + go func() { + val, err := s.reader.Storage(addr, storageKey) + if err != nil { + s.setError(err) + return + } + originStoragesCh <- &originStorage{ + addr, + storageKey, + val, + } + }() + } + } + + if pendingStorageCount == 0 { + return + } + for { + select { + case acctStorage := <-originStoragesCh: + s.originStorages[acctStorage.address][acctStorage.key] = acctStorage.value + pendingStorageCount-- + if pendingStorageCount == 0 { + return + } + } + } +} + // IntermediateRoot applies block state mutations and computes the updated state // trie root. func (s *BALStateTransition) IntermediateRoot(_ bool) common.Hash { @@ -383,20 +431,7 @@ func (s *BALStateTransition) IntermediateRoot(_ bool) common.Hash { s.originStoragesWG.Add(1) go func() { defer s.originStoragesWG.Done() - for _, addr := range s.accessList.ModifiedAccounts() { - diff := s.accessList.readAccountDiff(addr, lastIdx) - if len(diff.StorageWrites) > 0 { - s.originStorages[addr] = make(map[common.Hash]common.Hash) - for key := range diff.StorageWrites { - val, err := s.reader.Storage(addr, key) - if err != nil { - s.setError(err) - return - } - s.originStorages[addr][key] = val - } - } - } + s.loadOriginStorages() s.metrics.OriginStorageLoadTime = time.Since(start) }()