Skip to content

Commit

Permalink
Add override to avoid creating threads in StateDB.IntermediateRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
ajsutton committed Aug 23, 2024
1 parent 0f5b9dc commit 49c3d03
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
17 changes: 11 additions & 6 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"github.com/ethereum/go-ethereum/trie/triestate"
"github.com/ethereum/go-ethereum/trie/utils"
"github.com/holiman/uint256"
"golang.org/x/sync/errgroup"
)

// TriesInMemory represents the number of layers that are kept in RAM.
Expand Down Expand Up @@ -167,6 +166,8 @@ type StateDB struct {
StorageUpdated atomic.Int64
AccountDeleted int
StorageDeleted atomic.Int64

singlethreaded bool
}

// New creates a new state from a given trie.
Expand Down Expand Up @@ -196,6 +197,10 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
return sdb, nil
}

func (s *StateDB) MakeSingleThreaded() {
s.singlethreaded = true
}

// SetLogger sets the logger for account update hooks.
func (s *StateDB) SetLogger(l *tracing.Hooks) {
s.logger = l
Expand Down Expand Up @@ -851,9 +856,9 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
// method will internally call a blocking trie fetch from the prefetcher,
// so there's no need to explicitly wait for the prefetchers to finish.
var (
start = time.Now()
workers errgroup.Group
start = time.Now()
)
workers := newWorkerGroup(s.singlethreaded)
if s.db.TrieDB().IsVerkle() {
// Whilst MPT storage tries are independent, Verkle has one single trie
// for all the accounts and all the storage slots merged together. The
Expand Down Expand Up @@ -1225,10 +1230,10 @@ func (s *StateDB) commit(deleteEmptyObjects bool) (*stateUpdate, error) {
// off some milliseconds from the commit operation. Also accumulate the code
// writes to run in parallel with the computations.
var (
start = time.Now()
root common.Hash
workers errgroup.Group
start = time.Now()
root common.Hash
)
workers := newWorkerGroup(s.singlethreaded)
// Schedule the account trie first since that will be the biggest, so give
// it the most time to crunch.
//
Expand Down
37 changes: 37 additions & 0 deletions core/state/workers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package state

import (
"errors"

"golang.org/x/sync/errgroup"
)

type workerGroup interface {
Go(func() error)
SetLimit(int)
Wait() error
}

func newWorkerGroup(singlethreaded bool) workerGroup {
if singlethreaded {
return &inlineWorkerGroup{}
} else {
var grp errgroup.Group
return &grp
}
}

type inlineWorkerGroup struct {
err error
}

func (i *inlineWorkerGroup) Go(action func() error) {
i.err = errors.Join(i.err, action())
}

func (i *inlineWorkerGroup) SetLimit(_ int) {
}

func (i *inlineWorkerGroup) Wait() error {
return i.err
}

0 comments on commit 49c3d03

Please sign in to comment.