mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-03-05 10:55:02 +00:00
core/txpool/legacypool: reduce unnecessary allocations during add (#33701)
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:
parent
8e1de223ad
commit
bba41f8072
1 changed files with 15 additions and 19 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue