File tree Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Original file line number Diff line number Diff 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 ()
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments