mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-02-26 07:37:20 +00:00
core: use sync.Once for SenderCacher initialization (#31029)
This changes the SenderCacher so its goroutines will only be started on first use. Avoids starting them when package core is just imported but core.BlockChain isn't used.
This commit is contained in:
parent
9b68875d68
commit
9e4f08c25d
3 changed files with 13 additions and 4 deletions
|
|
@ -1619,7 +1619,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)
|
||||
SenderCacher().RecoverFromBlocks(types.MakeSigner(bc.chainConfig, chain[0].Number(), chain[0].Time()), chain)
|
||||
|
||||
var (
|
||||
stats = insertStats{startTime: mclock.Now()}
|
||||
|
|
|
|||
|
|
@ -18,12 +18,21 @@ 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())
|
||||
// senderCacherOnce is used to ensure that the SenderCacher is initialized only once.
|
||||
var senderCacherOnce = sync.OnceValue(func() *txSenderCacher {
|
||||
return newTxSenderCacher(runtime.NumCPU())
|
||||
})
|
||||
|
||||
// SenderCacher returns the singleton instance of SenderCacher, initializing it if called for the first time.
|
||||
// This function is thread-safe and ensures that initialization happens only once.
|
||||
func SenderCacher() *txSenderCacher {
|
||||
return senderCacherOnce()
|
||||
}
|
||||
|
||||
// txSenderCacherRequest is a request for recovering transaction senders with a
|
||||
// specific signature scheme and caching it into the transactions themselves.
|
||||
|
|
|
|||
|
|
@ -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.SenderCacher().Recover(pool.signer, reinject)
|
||||
pool.addTxsLocked(reinject, false)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue