bor initFrozenSnapshot: parallel erecover (#8488)

on 16-core mumbai's initFrozenSnapshot took 10min (to 38M block)
This commit is contained in:
Alex Sharov 2023-10-16 19:05:10 +07:00 committed by GitHub
parent ab04de6d9f
commit 21ebaab208
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,9 +17,11 @@ import (
"github.com/google/btree"
lru "github.com/hashicorp/golang-lru/arc/v2"
"github.com/ledgerwatch/erigon/eth/ethconfig/estimate"
"github.com/ledgerwatch/log/v3"
"github.com/xsleonard/go-merkle"
"golang.org/x/crypto/sha3"
"golang.org/x/sync/errgroup"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
@ -737,10 +739,25 @@ func (c *Bor) initFrozenSnapshot(chain consensus.ChainHeaderReader, number uint6
c.logger.Info("Stored proposer snapshot to disk", "number", 0, "hash", hash)
initialHeaders := make([]*types.Header, 0, 128)
g := errgroup.Group{}
g.SetLimit(estimate.AlmostAllCPUs())
defer g.Wait()
batchSize := 128 // must be < inmemorySignatures
initialHeaders := make([]*types.Header, 0, batchSize)
for i := uint64(1); i <= number; i++ {
header := chain.GetHeaderByNumber(i)
{
// `snap.apply` bottleneck - is recover of signer.
// to speedup: recover signer in background goroutines and save in `sigcache`
// `batchSize` < `inmemorySignatures`: means all current batch will fit in cache - and `snap.apply` will find it there.
snap := snap
g.Go(func() error {
_, _ = ecrecover(header, snap.sigcache, snap.config)
return nil
})
}
initialHeaders = append(initialHeaders, header)
if len(initialHeaders) == cap(initialHeaders) {
snap, err = snap.apply(initialHeaders, c.logger)