mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 02:40:45 +00:00
parent
35c905a131
commit
93d453c4c4
4 changed files with 33 additions and 6 deletions
|
|
@ -18,6 +18,7 @@ package txpool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/heap"
|
"container/heap"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/XinFinOrg/XDPoSChain/core/types"
|
"github.com/XinFinOrg/XDPoSChain/core/types"
|
||||||
|
|
@ -108,13 +109,14 @@ func (m *lendingtxSortedMap) Cap(threshold int) types.LendingTransactions {
|
||||||
// Otherwise gather and drop the highest nonce'd transactions
|
// Otherwise gather and drop the highest nonce'd transactions
|
||||||
var drops types.LendingTransactions
|
var drops types.LendingTransactions
|
||||||
|
|
||||||
sort.Sort(*m.index)
|
slices.Sort(*m.index)
|
||||||
for size := len(m.items); size > threshold; size-- {
|
for size := len(m.items); size > threshold; size-- {
|
||||||
drops = append(drops, m.items[(*m.index)[size-1]])
|
drops = append(drops, m.items[(*m.index)[size-1]])
|
||||||
delete(m.items, (*m.index)[size-1])
|
delete(m.items, (*m.index)[size-1])
|
||||||
}
|
}
|
||||||
*m.index = (*m.index)[:threshold]
|
*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
|
// If we had a cache, shift the back
|
||||||
if m.cache != nil {
|
if m.cache != nil {
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"container/heap"
|
"container/heap"
|
||||||
"math"
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
@ -159,13 +160,14 @@ func (m *sortedMap) Cap(threshold int) types.Transactions {
|
||||||
// Otherwise gather and drop the highest nonce'd transactions
|
// Otherwise gather and drop the highest nonce'd transactions
|
||||||
var drops types.Transactions
|
var drops types.Transactions
|
||||||
|
|
||||||
sort.Sort(*m.index)
|
slices.Sort(*m.index)
|
||||||
for size := len(m.items); size > threshold; size-- {
|
for size := len(m.items); size > threshold; size-- {
|
||||||
drops = append(drops, m.items[(*m.index)[size-1]])
|
drops = append(drops, m.items[(*m.index)[size-1]])
|
||||||
delete(m.items, (*m.index)[size-1])
|
delete(m.items, (*m.index)[size-1])
|
||||||
}
|
}
|
||||||
*m.index = (*m.index)[:threshold]
|
*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
|
// If we had a cache, shift the back
|
||||||
m.cacheMu.Lock()
|
m.cacheMu.Lock()
|
||||||
|
|
|
||||||
|
|
@ -68,3 +68,24 @@ func BenchmarkListAdd(t *testing.B) {
|
||||||
list.Filter(priceLimit, DefaultConfig.PriceBump, nil, nil)
|
list.Filter(priceLimit, DefaultConfig.PriceBump, nil, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
for b.Loop() {
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package txpool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/heap"
|
"container/heap"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/XinFinOrg/XDPoSChain/core/types"
|
"github.com/XinFinOrg/XDPoSChain/core/types"
|
||||||
|
|
@ -108,13 +109,14 @@ func (m *ordertxSortedMap) Cap(threshold int) types.OrderTransactions {
|
||||||
// Otherwise gather and drop the highest nonce'd transactions
|
// Otherwise gather and drop the highest nonce'd transactions
|
||||||
var drops types.OrderTransactions
|
var drops types.OrderTransactions
|
||||||
|
|
||||||
sort.Sort(*m.index)
|
slices.Sort(*m.index)
|
||||||
for size := len(m.items); size > threshold; size-- {
|
for size := len(m.items); size > threshold; size-- {
|
||||||
drops = append(drops, m.items[(*m.index)[size-1]])
|
drops = append(drops, m.items[(*m.index)[size-1]])
|
||||||
delete(m.items, (*m.index)[size-1])
|
delete(m.items, (*m.index)[size-1])
|
||||||
}
|
}
|
||||||
*m.index = (*m.index)[:threshold]
|
*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
|
// If we had a cache, shift the back
|
||||||
if m.cache != nil {
|
if m.cache != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue