Skip to content

Commit 051c571

Browse files
committed
core/txpool: remove a redundant heap.Init ethereum#28910
1 parent b49f6cb commit 051c571

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

core/txpool/lending_tx_list.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package txpool
1818

1919
import (
2020
"container/heap"
21+
"slices"
2122
"sort"
2223

2324
"github.com/XinFinOrg/XDPoSChain/core/types"
@@ -108,13 +109,14 @@ func (m *lendingtxSortedMap) Cap(threshold int) types.LendingTransactions {
108109
// Otherwise gather and drop the highest nonce'd transactions
109110
var drops types.LendingTransactions
110111

111-
sort.Sort(*m.index)
112+
slices.Sort(*m.index)
112113
for size := len(m.items); size > threshold; size-- {
113114
drops = append(drops, m.items[(*m.index)[size-1]])
114115
delete(m.items, (*m.index)[size-1])
115116
}
116117
*m.index = (*m.index)[:threshold]
117-
heap.Init(m.index)
118+
// The sorted m.index slice is still a valid heap, so there is no need to
119+
// reheap after deleting tail items.
118120

119121
// If we had a cache, shift the back
120122
if m.cache != nil {

core/txpool/list.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"container/heap"
2121
"math"
2222
"math/big"
23+
"slices"
2324
"sort"
2425
"sync"
2526
"sync/atomic"
@@ -150,13 +151,14 @@ func (m *sortedMap) Cap(threshold int) types.Transactions {
150151
// Otherwise gather and drop the highest nonce'd transactions
151152
var drops types.Transactions
152153

153-
sort.Sort(*m.index)
154+
slices.Sort(*m.index)
154155
for size := len(m.items); size > threshold; size-- {
155156
drops = append(drops, m.items[(*m.index)[size-1]])
156157
delete(m.items, (*m.index)[size-1])
157158
}
158159
*m.index = (*m.index)[:threshold]
159-
heap.Init(m.index)
160+
// The sorted m.index slice is still a valid heap, so there is no need to
161+
// reheap after deleting tail items.
160162

161163
// If we had a cache, shift the back
162164
if m.cache != nil {

core/txpool/list_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,24 @@ func BenchmarkListAdd(t *testing.B) {
6868
list.Filter(priceLimit, DefaultConfig.PriceBump, nil, nil)
6969
}
7070
}
71+
72+
func BenchmarkListCapOneTx(b *testing.B) {
73+
// Generate a list of transactions to insert
74+
key, _ := crypto.GenerateKey()
75+
76+
txs := make(types.Transactions, 32)
77+
for i := 0; i < len(txs); i++ {
78+
txs[i] = transaction(uint64(i), 0, key)
79+
}
80+
81+
for b.Loop() {
82+
list := newList(true)
83+
// Insert the transactions in a random order
84+
for _, v := range rand.Perm(len(txs)) {
85+
list.Add(txs[v], DefaultConfig.PriceBump)
86+
}
87+
b.StartTimer()
88+
list.Cap(list.Len() - 1)
89+
b.StopTimer()
90+
}
91+
}

core/txpool/order_tx_list.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package txpool
1818

1919
import (
2020
"container/heap"
21+
"slices"
2122
"sort"
2223

2324
"github.com/XinFinOrg/XDPoSChain/core/types"
@@ -108,13 +109,14 @@ func (m *ordertxSortedMap) Cap(threshold int) types.OrderTransactions {
108109
// Otherwise gather and drop the highest nonce'd transactions
109110
var drops types.OrderTransactions
110111

111-
sort.Sort(*m.index)
112+
slices.Sort(*m.index)
112113
for size := len(m.items); size > threshold; size-- {
113114
drops = append(drops, m.items[(*m.index)[size-1]])
114115
delete(m.items, (*m.index)[size-1])
115116
}
116117
*m.index = (*m.index)[:threshold]
117-
heap.Init(m.index)
118+
// The sorted m.index slice is still a valid heap, so there is no need to
119+
// reheap after deleting tail items.
118120

119121
// If we had a cache, shift the back
120122
if m.cache != nil {

0 commit comments

Comments
 (0)