Skip to content

Commit

Permalink
core/txpool/legacypool: remove a redundant heap.Init (ethereum#28910)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin HS <martin@swende.se>
Co-authored-by: Felix Lange <fjl@twurst.com>
  • Loading branch information
3 people authored Feb 15, 2024
1 parent 1bdf8b9 commit a193bb0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 4 additions & 3 deletions core/txpool/legacypool/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/holiman/uint256"
"golang.org/x/exp/slices"
)

// nonceHeap is a heap.Interface implementation over 64bit unsigned integers for
Expand Down Expand Up @@ -160,14 +161,14 @@ func (m *sortedMap) Cap(threshold int) types.Transactions {
}
// Otherwise gather and drop the highest nonce'd transactions
var drops types.Transactions

sort.Sort(*m.index)
slices.Sort(*m.index)
for size := len(m.items); size > threshold; size-- {
drops = append(drops, m.items[(*m.index)[size-1]])
delete(m.items, (*m.index)[size-1])
}
*m.index = (*m.index)[:threshold]
heap.Init(m.index)
// The sorted m.index slice is still a valid heap, so there is no need to
// reheap after deleting tail items.

// If we had a cache, shift the back
m.cacheMu.Lock()
Expand Down
22 changes: 22 additions & 0 deletions core/txpool/legacypool/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,25 @@ func BenchmarkListAdd(b *testing.B) {
}
}
}

func BenchmarkListCapOneTx(b *testing.B) {
// Generate a list of transactions to insert
key, _ := crypto.GenerateKey()

txs := make(types.Transactions, 32)
for i := 0; i < len(txs); i++ {
txs[i] = transaction(uint64(i), 0, key)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
list := newList(true)
// Insert the transactions in a random order
for _, v := range rand.Perm(len(txs)) {
list.Add(txs[v], DefaultConfig.PriceBump)
}
b.StartTimer()
list.Cap(list.Len() - 1)
b.StopTimer()
}
}

0 comments on commit a193bb0

Please sign in to comment.