Skip to content

Commit 11f7139

Browse files
core: added tx.GasPriceIntCmp for comparison without allocation
adds a method to remove unneeded allocation in comparison to tx.gasPrice
1 parent 79b0259 commit 11f7139

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

core/tx_list.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (l *txList) Add(tx *types.Transaction, priceBump uint64) (bool, *types.Tran
267267
// Have to ensure that the new gas price is higher than the old gas
268268
// price as well as checking the percentage threshold to ensure that
269269
// this is accurate for low (Wei-level) gas price replacements
270-
if old.CmpGasPrice(tx) >= 0 || threshold.Cmp(tx.GasPrice()) > 0 {
270+
if old.GasPriceCmp(tx) >= 0 || tx.GasPriceIntCmp(threshold) < 0 {
271271
return false, nil
272272
}
273273
}
@@ -397,7 +397,7 @@ func (h priceHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
397397

398398
func (h priceHeap) Less(i, j int) bool {
399399
// Sort primarily by price, returning the cheaper one
400-
switch h[i].CmpGasPrice(h[j]) {
400+
switch h[i].GasPriceCmp(h[j]) {
401401
case -1:
402402
return true
403403
case 1:
@@ -474,7 +474,7 @@ func (l *txPricedList) Cap(threshold *big.Int, local *accountSet) types.Transact
474474
continue
475475
}
476476
// Stop the discards if we've reached the threshold
477-
if tx.GasPrice().Cmp(threshold) >= 0 {
477+
if tx.GasPriceIntCmp(threshold) >= 0 {
478478
save = append(save, tx)
479479
break
480480
}
@@ -514,7 +514,7 @@ func (l *txPricedList) Underpriced(tx *types.Transaction, local *accountSet) boo
514514
return false
515515
}
516516
cheapest := []*types.Transaction(*l.items)[0]
517-
return cheapest.CmpGasPrice(tx) >= 0
517+
return cheapest.GasPriceCmp(tx) >= 0
518518
}
519519

520520
// Discard finds a number of most underpriced transactions, removes them from the

core/tx_list_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,21 @@ func TestStrictTxListAdd(t *testing.T) {
4949
}
5050
}
5151
}
52+
53+
func BenchmarkTxListAdd(t *testing.B) {
54+
// Generate a list of transactions to insert
55+
key, _ := crypto.GenerateKey()
56+
57+
txs := make(types.Transactions, 100000)
58+
for i := 0; i < len(txs); i++ {
59+
txs[i] = transaction(uint64(i), 0, key)
60+
}
61+
// Insert the transactions in a random order
62+
list := newTxList(true)
63+
priceLimit := DefaultTxPoolConfig.PriceLimit
64+
t.ResetTimer()
65+
for _, v := range rand.Perm(len(txs)) {
66+
list.Add(txs[v], DefaultTxPoolConfig.PriceBump)
67+
list.Filter(priceLimit, DefaultTxPoolConfig.PriceBump)
68+
}
69+
}

core/tx_pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
534534
}
535535
// Drop non-local transactions under our own minimal accepted gas price
536536
local = local || pool.locals.contains(from) // account may be local even if the transaction arrived from the network
537-
if !local && pool.gasPrice.Cmp(tx.GasPrice()) > 0 {
537+
if !local && tx.GasPriceIntCmp(pool.gasPrice) < 0 {
538538
return ErrUnderpriced
539539
}
540540
// Ensure the transaction adheres to nonce ordering

core/types/transaction.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,13 @@ func (tx *Transaction) GasPrice() *big.Int { return new(big.Int).Set(tx.data.Pri
180180
func (tx *Transaction) GasPriceCmp(other *Transaction) int {
181181
return tx.data.Price.Cmp(other.data.Price)
182182
}
183+
func (tx *Transaction) GasPriceIntCmp(other *big.Int) int {
184+
return tx.data.Price.Cmp(other)
185+
}
183186
func (tx *Transaction) Value() *big.Int { return new(big.Int).Set(tx.data.Amount) }
184187
func (tx *Transaction) Nonce() uint64 { return tx.data.AccountNonce }
185188
func (tx *Transaction) CheckNonce() bool { return true }
186189

187-
func (tx *Transaction) CmpGasPrice(other *Transaction) int {
188-
return tx.data.Price.Cmp(other.data.Price)
189-
}
190-
191190
// To returns the recipient address of the transaction.
192191
// It returns nil if the transaction is a contract creation.
193192
func (tx *Transaction) To() *common.Address {

eth/gasprice/gasprice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ type transactionsByGasPrice []*types.Transaction
156156

157157
func (t transactionsByGasPrice) Len() int { return len(t) }
158158
func (t transactionsByGasPrice) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
159-
func (t transactionsByGasPrice) Less(i, j int) bool { return t[i].CmpGasPrice(t[j]) < 0 }
159+
func (t transactionsByGasPrice) Less(i, j int) bool { return t[i].GasPriceCmp(t[j]) < 0 }
160160

161161
// getBlockPrices calculates the lowest transaction gas price in a given block
162162
// and sends it to the result channel. If the block is empty, price is nil.

0 commit comments

Comments
 (0)