Skip to content

Commit 00493d1

Browse files
colinlyguoholimanfjl
authored andcommitted
core/txpool/legacypool: remove a redundant heap.Init (ethereum#28910)
Co-authored-by: Martin HS <[email protected]> Co-authored-by: Felix Lange <[email protected]>
1 parent 27705ad commit 00493d1

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

core/txpool/legacypool/list.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ethereum/go-ethereum/common"
2929
"github.com/ethereum/go-ethereum/core/types"
3030
"github.com/holiman/uint256"
31+
"golang.org/x/exp/slices"
3132
)
3233

3334
// nonceHeap is a heap.Interface implementation over 64bit unsigned integers for
@@ -160,14 +161,14 @@ func (m *sortedMap) Cap(threshold int) types.Transactions {
160161
}
161162
// Otherwise gather and drop the highest nonce'd transactions
162163
var drops types.Transactions
163-
164-
sort.Sort(*m.index)
164+
slices.Sort(*m.index)
165165
for size := len(m.items); size > threshold; size-- {
166166
drops = append(drops, m.items[(*m.index)[size-1]])
167167
delete(m.items, (*m.index)[size-1])
168168
}
169169
*m.index = (*m.index)[:threshold]
170-
heap.Init(m.index)
170+
// The sorted m.index slice is still a valid heap, so there is no need to
171+
// reheap after deleting tail items.
171172

172173
// If we had a cache, shift the back
173174
m.cacheMu.Lock()

core/txpool/legacypool/list_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,25 @@ func BenchmarkListAdd(b *testing.B) {
8787
}
8888
}
8989
}
90+
91+
func BenchmarkListCapOneTx(b *testing.B) {
92+
// Generate a list of transactions to insert
93+
key, _ := crypto.GenerateKey()
94+
95+
txs := make(types.Transactions, 32)
96+
for i := 0; i < len(txs); i++ {
97+
txs[i] = transaction(uint64(i), 0, key)
98+
}
99+
100+
b.ResetTimer()
101+
for i := 0; i < b.N; i++ {
102+
list := newList(true)
103+
// Insert the transactions in a random order
104+
for _, v := range rand.Perm(len(txs)) {
105+
list.Add(txs[v], DefaultConfig.PriceBump)
106+
}
107+
b.StartTimer()
108+
list.Cap(list.Len() - 1)
109+
b.StopTimer()
110+
}
111+
}

0 commit comments

Comments
 (0)