improve txSenderCacher init

This commit is contained in:
georgehao 2025-01-14 09:03:59 +08:00
parent 864e717b56
commit d078ad321c
No known key found for this signature in database
3 changed files with 20 additions and 4 deletions

View file

@ -1616,7 +1616,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool, makeWitness
return nil, 0, nil
}
// Start a parallel signature recovery (signer will fluke on fork transition, minimal perf loss)
SenderCacher.RecoverFromBlocks(types.MakeSigner(bc.chainConfig, chain[0].Number(), chain[0].Time()), chain)
GetSenderCacher().RecoverFromBlocks(types.MakeSigner(bc.chainConfig, chain[0].Number(), chain[0].Time()), chain)
var (
stats = insertStats{startTime: mclock.Now()}

View file

@ -18,12 +18,28 @@ package core
import (
"runtime"
"sync"
"github.com/ethereum/go-ethereum/core/types"
)
// SenderCacher is a concurrent transaction sender recoverer and cacher.
var SenderCacher = newTxSenderCacher(runtime.NumCPU())
var (
// senderCacherOnce is used to ensure that the SenderCacher is initialized only once.
// It leverages sync.Once to provide thread-safe lazy initialization.
senderCacherOnce sync.Once
// senderCacherInstance is a concurrent transaction sender recoverer and cacher.
senderCacherInstance = newTxSenderCacher(runtime.NumCPU())
)
// GetSenderCacher returns the singleton instance of SenderCacher.
// If the instance has not been initialized yet, it will be created using newTxSenderCacher.
// This function is thread-safe and ensures that initialization happens only once.
func GetSenderCacher() *txSenderCacher {
senderCacherOnce.Do(func() {
senderCacherInstance = newTxSenderCacher(runtime.NumCPU())
})
return senderCacherInstance
}
// txSenderCacherRequest is a request for recovering transaction senders with a
// specific signature scheme and caching it into the transactions themselves.

View file

@ -1440,7 +1440,7 @@ func (pool *LegacyPool) reset(oldHead, newHead *types.Header) {
// Inject any transactions discarded due to reorgs
log.Debug("Reinjecting stale transactions", "count", len(reinject))
core.SenderCacher.Recover(pool.signer, reinject)
core.GetSenderCacher().Recover(pool.signer, reinject)
pool.addTxsLocked(reinject, false)
}