File tree 2 files changed +26
-3
lines changed
2 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ import (
28
28
"github.com/ethereum/go-ethereum/common"
29
29
"github.com/ethereum/go-ethereum/core/types"
30
30
"github.com/holiman/uint256"
31
+ "golang.org/x/exp/slices"
31
32
)
32
33
33
34
// nonceHeap is a heap.Interface implementation over 64bit unsigned integers for
@@ -160,14 +161,14 @@ func (m *sortedMap) Cap(threshold int) types.Transactions {
160
161
}
161
162
// Otherwise gather and drop the highest nonce'd transactions
162
163
var drops types.Transactions
163
-
164
- sort .Sort (* m .index )
164
+ slices .Sort (* m .index )
165
165
for size := len (m .items ); size > threshold ; size -- {
166
166
drops = append (drops , m .items [(* m .index )[size - 1 ]])
167
167
delete (m .items , (* m .index )[size - 1 ])
168
168
}
169
169
* 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.
171
172
172
173
// If we had a cache, shift the back
173
174
m .cacheMu .Lock ()
Original file line number Diff line number Diff line change @@ -87,3 +87,25 @@ func BenchmarkListAdd(b *testing.B) {
87
87
}
88
88
}
89
89
}
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