Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/txpool/legacypool: remove a redundant heap.Init #28910

Merged
merged 11 commits into from
Feb 15, 2024
8 changes: 3 additions & 5 deletions core/txpool/legacypool/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,11 @@ 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)
for size := len(m.items); size > threshold; size-- {
drops = append(drops, m.items[(*m.index)[size-1]])
delete(m.items, (*m.index)[size-1])
dropIdx := m.index.Pop().(uint64)
colinlyguo marked this conversation as resolved.
Show resolved Hide resolved
colinlyguo marked this conversation as resolved.
Show resolved Hide resolved
drops = append(drops, m.items[dropIdx])
delete(m.items, dropIdx)
}
*m.index = (*m.index)[:threshold]
heap.Init(m.index)
colinlyguo marked this conversation as resolved.
Show resolved Hide resolved

// 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 @@ -70,3 +70,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()
}
}