From dfefd8eb7be4c16d253fadaa74d9a077f28956c9 Mon Sep 17 00:00:00 2001 From: ucwong Date: Wed, 4 Feb 2026 18:10:41 +0800 Subject: [PATCH] reduce unnecessary allocations --- core/txpool/txpool.go | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/core/txpool/txpool.go b/core/txpool/txpool.go index cf2049042b..62526e885a 100644 --- a/core/txpool/txpool.go +++ b/core/txpool/txpool.go @@ -943,8 +943,8 @@ func (pool *TxPool) addTxs(txs []*types.Transaction, local, 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 @@ -962,25 +962,16 @@ func (pool *TxPool) addTxs(txs []*types.Transaction, local, 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, local) + dirtyAddrs := pool.addTxsLocked(txs, errs, local) 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 { @@ -991,13 +982,15 @@ func (pool *TxPool) addTxs(txs []*types.Transaction, local, sync bool) []error { // addTxsLocked attempts to queue a batch of transactions if they are valid. // The transaction pool lock must be held. -func (pool *TxPool) addTxsLocked(txs []*types.Transaction, local bool) ([]error, *accountSet) { +func (pool *TxPool) addTxsLocked(txs []*types.Transaction, errs []error, local bool) *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, local) errs[i] = err if err == nil { @@ -1008,7 +1001,7 @@ func (pool *TxPool) addTxsLocked(txs []*types.Transaction, local bool) ([]error, } } validTxMeter.Mark(valid) - return errs, dirty + return dirty } // Status returns the status (unknown/pending/queued) of a batch of transactions @@ -1380,7 +1373,7 @@ func (pool *TxPool) 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, false) + pool.addTxsLocked(reinject, make([]error, len(reinject)), false) // Update all fork indicator by next pending block number. next := new(big.Int).Add(newHead.Number, big.NewInt(1))