mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 13:46:43 +00:00
reuse the cache if it is not null
This commit is contained in:
parent
b369a855fb
commit
50958bac5c
1 changed files with 23 additions and 14 deletions
|
|
@ -78,11 +78,19 @@ func (m *SortedMap) Get(nonce uint64) *types.Transaction {
|
||||||
// index. If a transaction already exists with the same nonce, it's overwritten.
|
// index. If a transaction already exists with the same nonce, it's overwritten.
|
||||||
func (m *SortedMap) Put(tx *types.Transaction) {
|
func (m *SortedMap) Put(tx *types.Transaction) {
|
||||||
nonce := tx.Nonce()
|
nonce := tx.Nonce()
|
||||||
if m.items[nonce] == nil {
|
// If the tx already exit, return immediately.
|
||||||
heap.Push(m.index, nonce)
|
if m.items[nonce] != nil {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m.items[nonce] = tx
|
||||||
|
heap.Push(m.index, nonce)
|
||||||
|
|
||||||
m.cacheMu.Lock()
|
m.cacheMu.Lock()
|
||||||
m.items[nonce], m.cache = tx, nil
|
if m.cache != nil {
|
||||||
|
m.cache = append(m.cache, tx)
|
||||||
|
sort.Sort(types.TxByNonce(m.cache))
|
||||||
|
}
|
||||||
m.cacheMu.Unlock()
|
m.cacheMu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -162,9 +170,9 @@ 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
|
||||||
slices.Sort(*m.index)
|
slices.Sort(*m.index)
|
||||||
for size := len(m.items); size > threshold; size-- {
|
for _, nonce := range (*m.index)[threshold:] {
|
||||||
drops = append(drops, m.items[(*m.index)[size-1]])
|
drops = append(drops, m.items[nonce])
|
||||||
delete(m.items, (*m.index)[size-1])
|
delete(m.items, nonce)
|
||||||
}
|
}
|
||||||
*m.index = (*m.index)[:threshold]
|
*m.index = (*m.index)[:threshold]
|
||||||
// The sorted m.index slice is still a valid heap, so there is no need to
|
// The sorted m.index slice is still a valid heap, so there is no need to
|
||||||
|
|
@ -173,7 +181,7 @@ func (m *SortedMap) Cap(threshold int) types.Transactions {
|
||||||
// If we had a cache, shift the back
|
// If we had a cache, shift the back
|
||||||
m.cacheMu.Lock()
|
m.cacheMu.Lock()
|
||||||
if m.cache != nil {
|
if m.cache != nil {
|
||||||
m.cache = m.cache[:len(m.cache)-len(drops)]
|
m.cache = m.cache[:threshold]
|
||||||
}
|
}
|
||||||
m.cacheMu.Unlock()
|
m.cacheMu.Unlock()
|
||||||
return drops
|
return drops
|
||||||
|
|
@ -188,15 +196,16 @@ func (m *SortedMap) Remove(nonce uint64) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// Otherwise delete the transaction and fix the heap index
|
// Otherwise delete the transaction and fix the heap index
|
||||||
for i := 0; i < m.index.Len(); i++ {
|
index := slices.Index(*m.index, nonce)
|
||||||
if (*m.index)[i] == nonce {
|
heap.Remove(m.index, index)
|
||||||
heap.Remove(m.index, i)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
delete(m.items, nonce)
|
delete(m.items, nonce)
|
||||||
|
|
||||||
m.cacheMu.Lock()
|
m.cacheMu.Lock()
|
||||||
m.cache = nil
|
if m.cache != nil {
|
||||||
|
slices.DeleteFunc(m.cache, func(tx *types.Transaction) bool {
|
||||||
|
return tx.Nonce() == nonce
|
||||||
|
})
|
||||||
|
}
|
||||||
m.cacheMu.Unlock()
|
m.cacheMu.Unlock()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue