core/txpool/blobpool: improve newPriceHeap function (#30050)

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
maskpp 2024-06-28 21:51:27 +08:00 committed by GitHub
parent 19c3c1e205
commit 36d67be41b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -17,13 +17,13 @@
package blobpool package blobpool
import ( import (
"bytes"
"container/heap" "container/heap"
"math" "math"
"sort" "slices"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256" "github.com/holiman/uint256"
"golang.org/x/exp/maps"
) )
// evictHeap is a helper data structure to keep track of the cheapest bottleneck // evictHeap is a helper data structure to keep track of the cheapest bottleneck
@ -49,20 +49,15 @@ type evictHeap struct {
func newPriceHeap(basefee *uint256.Int, blobfee *uint256.Int, index map[common.Address][]*blobTxMeta) *evictHeap { func newPriceHeap(basefee *uint256.Int, blobfee *uint256.Int, index map[common.Address][]*blobTxMeta) *evictHeap {
heap := &evictHeap{ heap := &evictHeap{
metas: index, metas: index,
index: make(map[common.Address]int), index: make(map[common.Address]int, len(index)),
} }
// Populate the heap in account sort order. Not really needed in practice, // Populate the heap in account sort order. Not really needed in practice,
// but it makes the heap initialization deterministic and less annoying to // but it makes the heap initialization deterministic and less annoying to
// test in unit tests. // test in unit tests.
addrs := make([]common.Address, 0, len(index)) heap.addrs = maps.Keys(index)
for addr := range index { slices.SortFunc(heap.addrs, common.Address.Cmp)
addrs = append(addrs, addr) for i, addr := range heap.addrs {
} heap.index[addr] = i
sort.Slice(addrs, func(i, j int) bool { return bytes.Compare(addrs[i][:], addrs[j][:]) < 0 })
for _, addr := range addrs {
heap.index[addr] = len(heap.addrs)
heap.addrs = append(heap.addrs, addr)
} }
heap.reinit(basefee, blobfee, true) heap.reinit(basefee, blobfee, true)
return heap return heap