mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-17 21:46:36 +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 {
|
func (pool *LegacyPool) Add(txs []*types.Transaction, sync bool) []error {
|
||||||
// Filter out known ones without obtaining the pool lock or recovering signatures
|
// Filter out known ones without obtaining the pool lock or recovering signatures
|
||||||
var (
|
var (
|
||||||
errs = make([]error, len(txs))
|
hasValid bool
|
||||||
news = make([]*types.Transaction, 0, len(txs))
|
errs = make([]error, len(txs))
|
||||||
)
|
)
|
||||||
for i, tx := range txs {
|
for i, tx := range txs {
|
||||||
// If the transaction is known, pre-set the error slot
|
// 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)
|
invalidTxMeter.Mark(1)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Accumulate all unknown transactions for deeper processing
|
hasValid = true
|
||||||
news = append(news, tx)
|
|
||||||
}
|
}
|
||||||
if len(news) == 0 {
|
if !hasValid {
|
||||||
return errs
|
return errs
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process all the new transaction and merge any errors into the original slice
|
// Process all the new transaction and merge any errors into the original slice
|
||||||
pool.mu.Lock()
|
pool.mu.Lock()
|
||||||
newErrs, dirtyAddrs := pool.addTxsLocked(news)
|
dirtyAddrs := pool.addTxsLocked(txs, errs)
|
||||||
pool.mu.Unlock()
|
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
|
// Reorg the pool internals if needed and return
|
||||||
done := pool.requestPromoteExecutables(dirtyAddrs)
|
done := pool.requestPromoteExecutables(dirtyAddrs)
|
||||||
if sync {
|
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.
|
// addTxsLocked attempts to queue a batch of transactions if they are valid.
|
||||||
// The transaction pool lock must be held.
|
// The transaction pool lock must be held.
|
||||||
// Returns the error for each tx, and the set of accounts that might became promotable.
|
// Sets the error for each tx, and the set of accounts that might became promotable.
|
||||||
func (pool *LegacyPool) addTxsLocked(txs []*types.Transaction) ([]error, *accountSet) {
|
// 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 (
|
var (
|
||||||
dirty = newAccountSet(pool.signer)
|
dirty = newAccountSet(pool.signer)
|
||||||
errs = make([]error, len(txs))
|
|
||||||
valid int64
|
valid int64
|
||||||
)
|
)
|
||||||
for i, tx := range txs {
|
for i, tx := range txs {
|
||||||
|
if errs[i] != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
replaced, err := pool.add(tx)
|
replaced, err := pool.add(tx)
|
||||||
errs[i] = err
|
errs[i] = err
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
@ -975,7 +971,7 @@ func (pool *LegacyPool) addTxsLocked(txs []*types.Transaction) ([]error, *accoun
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
validTxMeter.Mark(valid)
|
validTxMeter.Mark(valid)
|
||||||
return errs, dirty
|
return dirty
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status returns the status (unknown/pending/queued) of a batch of transactions
|
// 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
|
// Inject any transactions discarded due to reorgs
|
||||||
log.Debug("Reinjecting stale transactions", "count", len(reinject))
|
log.Debug("Reinjecting stale transactions", "count", len(reinject))
|
||||||
core.SenderCacher().Recover(pool.signer, 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
|
// promoteExecutables moves transactions that have become processable from the
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue