core/txpool/legacypool: reduce unnecessary allocations during add (#33701)
Some checks are pending
/ Linux Build (push) Waiting to run
/ Linux Build (arm) (push) Waiting to run
/ Keeper Build (push) Waiting to run
/ Windows Build (push) Waiting to run
/ Docker Image (push) Waiting to run

Not many allocs we save here, but it also cleans up the logic and should
speed up the process a tiny bit
This commit is contained in:
Marius van der Wijden 2026-02-04 04:50:19 +01:00 committed by GitHub
parent 8e1de223ad
commit bba41f8072
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -908,8 +908,8 @@ func (pool *LegacyPool) addRemoteSync(tx *types.Transaction) error {
func (pool *LegacyPool) Add(txs []*types.Transaction, sync bool) []error {
// Filter out known ones without obtaining the pool lock or recovering signatures
var (
errs = make([]error, len(txs))
news = make([]*types.Transaction, 0, len(txs))
hasValid bool
errs = make([]error, len(txs))
)
for i, tx := range txs {
// If the transaction is known, pre-set the error slot
@ -927,26 +927,17 @@ func (pool *LegacyPool) Add(txs []*types.Transaction, sync bool) []error {
invalidTxMeter.Mark(1)
continue
}
// Accumulate all unknown transactions for deeper processing
news = append(news, tx)
hasValid = true
}
if len(news) == 0 {
if !hasValid {
return errs
}
// Process all the new transaction and merge any errors into the original slice
pool.mu.Lock()
newErrs, dirtyAddrs := pool.addTxsLocked(news)
dirtyAddrs := pool.addTxsLocked(txs, errs)
pool.mu.Unlock()
var nilSlot = 0
for _, err := range newErrs {
for errs[nilSlot] != nil {
nilSlot++
}
errs[nilSlot] = err
nilSlot++
}
// Reorg the pool internals if needed and return
done := pool.requestPromoteExecutables(dirtyAddrs)
if sync {
@ -957,14 +948,19 @@ func (pool *LegacyPool) Add(txs []*types.Transaction, sync bool) []error {
// addTxsLocked attempts to queue a batch of transactions if they are valid.
// The transaction pool lock must be held.
// Returns the error for each tx, and the set of accounts that might became promotable.
func (pool *LegacyPool) addTxsLocked(txs []*types.Transaction) ([]error, *accountSet) {
// Sets the error for each tx, and the set of accounts that might became promotable.
// We only try to add txs that have no error set in the errs slice.
// If adding the transaction returns an error, we set the error in the errs slice.
// Requires len(txs) == len(errs).
func (pool *LegacyPool) addTxsLocked(txs []*types.Transaction, errs []error) *accountSet {
var (
dirty = newAccountSet(pool.signer)
errs = make([]error, len(txs))
valid int64
)
for i, tx := range txs {
if errs[i] != nil {
continue
}
replaced, err := pool.add(tx)
errs[i] = err
if err == nil {
@ -975,7 +971,7 @@ func (pool *LegacyPool) addTxsLocked(txs []*types.Transaction) ([]error, *accoun
}
}
validTxMeter.Mark(valid)
return errs, dirty
return dirty
}
// Status returns the status (unknown/pending/queued) of a batch of transactions
@ -1397,7 +1393,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)
pool.addTxsLocked(reinject)
pool.addTxsLocked(reinject, make([]error, len(reinject)))
}
// promoteExecutables moves transactions that have become processable from the