Skip to content

Commit

Permalink
feat(txpool): improve performance of Reheap (bnb-chain#89)
Browse files Browse the repository at this point in the history
Co-authored-by: andyzhang2023 <andyzhang2023@gmail.com>
  • Loading branch information
andyzhang2023 and andyzhang2023 authored May 28, 2024
1 parent 0380943 commit 3949d08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions core/txpool/legacypool/legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ func New(config Config, chain BlockChain) *LegacyPool {
chain: chain,
chainconfig: chain.Config(),
signer: types.LatestSigner(chain.Config()),
pending: make(map[common.Address]*list),
queue: make(map[common.Address]*list),
pending: make(map[common.Address]*list, config.GlobalSlots),
queue: make(map[common.Address]*list, config.GlobalQueue),
beats: make(map[common.Address]time.Time),
all: newLookup(),
reqResetCh: make(chan *txpoolResetRequest),
Expand Down
15 changes: 14 additions & 1 deletion core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,20 @@ func (tx *Transaction) EffectiveGasTipCmp(other *Transaction, baseFee *big.Int)
if baseFee == nil {
return tx.GasTipCapCmp(other)
}
return tx.EffectiveGasTipValue(baseFee).Cmp(other.EffectiveGasTipValue(baseFee))
// the EffectiveGasTipValue() always copies two big.Int, which cost almost 90% cpu resource of the whole function,
// so we define an alternative function to improve the performance.
return effectiveGasTipValue(tx, baseFee).Cmp(effectiveGasTipValue(other, baseFee))
}

func effectiveGasTipValue(tx *Transaction, baseFee *big.Int) *big.Int {
if tx.Type() == DepositTxType {
return new(big.Int)
}
if baseFee == nil {
return tx.inner.gasTipCap()
}
gasFeeCap := tx.inner.gasFeeCap()
return math.BigMin(tx.inner.gasTipCap(), new(big.Int).Sub(gasFeeCap, baseFee))
}

// EffectiveGasTipIntCmp compares the effective gasTipCap of a transaction to the given gasTipCap.
Expand Down

0 comments on commit 3949d08

Please sign in to comment.