Skip to content

Commit 3f4c3a4

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

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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+
}

0 commit comments

Comments
 (0)