Skip to content

Commit

Permalink
triedb/pathdb: parallelize add layer
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Dec 2, 2024
1 parent 2414939 commit 44762fe
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions triedb/pathdb/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package pathdb

import (
"fmt"
"runtime"
"sync"
"time"

"github.com/ethereum/go-ethereum/common"
"golang.org/x/sync/errgroup"
)

// slicePool is a shared pool of hash slice, for reducing the GC pressure.
Expand Down Expand Up @@ -105,24 +107,34 @@ func (l *lookup) addLayer(diff *diffLayer) {
lookupAddLayerTimer.UpdateSince(now)
}(time.Now())

// TODO(rjl493456442) theoretically the code below could be parallelized,
// but it will slow down the other parts of system (e.g., EVM execution)
// with unknown reasons.
state := diff.rootHash()
var (
state = diff.rootHash()
lock sync.Mutex
workers errgroup.Group
)
workers.SetLimit(runtime.NumCPU() / 2)

for accountHash, nodes := range diff.nodes.nodes {
subset := l.nodes[accountHash]
if subset == nil {
subset = make(map[string][]common.Hash)
l.nodes[accountHash] = subset
}
// Put the layer hash at the end of the list
for path := range nodes {
if _, exists := subset[path]; !exists {
subset[path] = getSlice()
workers.Go(func() error {
lock.Lock()
subset := l.nodes[accountHash]
if subset == nil {
subset = make(map[string][]common.Hash)
l.nodes[accountHash] = subset
}
subset[path] = append(subset[path], state)
}
lock.Unlock()

// Put the layer hash at the end of the list
for path := range nodes {
if _, exists := subset[path]; !exists {
subset[path] = getSlice()
}
subset[path] = append(subset[path], state)
}
return nil
})
}
workers.Wait()
}

// removeLayer traverses all the dirty nodes within the given diff layer and
Expand Down

0 comments on commit 44762fe

Please sign in to comment.