legacypool: reduce unnecessary allocations during add

This commit is contained in:
MariusVanDerWijden 2026-01-28 16:47:33 +01:00
parent 344d01e2be
commit ad1d943c7d

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 {
@ -958,13 +949,16 @@ 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) {
// 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 +969,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 +1391,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