From 02ee6235489117508c4b325db19443c874c459c8 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Tue, 8 Oct 2024 10:21:22 +0200 Subject: [PATCH] all: combine key iterators and sorting in one step --- core/txpool/blobpool/evictheap.go | 3 +-- p2p/netutil/net.go | 3 +-- triedb/pathdb/history.go | 8 ++------ 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/core/txpool/blobpool/evictheap.go b/core/txpool/blobpool/evictheap.go index 0792fdf388..722a71bc9b 100644 --- a/core/txpool/blobpool/evictheap.go +++ b/core/txpool/blobpool/evictheap.go @@ -54,8 +54,7 @@ func newPriceHeap(basefee *uint256.Int, blobfee *uint256.Int, index map[common.A // Populate the heap in account sort order. Not really needed in practice, // but it makes the heap initialization deterministic and less annoying to // test in unit tests. - heap.addrs = slices.Collect(maps.Keys(index)) - slices.SortFunc(heap.addrs, common.Address.Cmp) + heap.addrs = slices.SortedFunc(maps.Keys(index), common.Address.Cmp) for i, addr := range heap.addrs { heap.index[addr] = i } diff --git a/p2p/netutil/net.go b/p2p/netutil/net.go index f98f2ba14f..696c331859 100644 --- a/p2p/netutil/net.go +++ b/p2p/netutil/net.go @@ -323,8 +323,7 @@ func (s *DistinctNetSet) key(ip netip.Addr) netip.Prefix { // String implements fmt.Stringer func (s DistinctNetSet) String() string { - keys := slices.Collect(maps.Keys(s.members)) - slices.SortFunc(keys, func(a, b netip.Prefix) int { + keys := slices.SortedFunc(maps.Keys(s.members), func(a, b netip.Prefix) int { return strings.Compare(a.String(), b.String()) }) diff --git a/triedb/pathdb/history.go b/triedb/pathdb/history.go index d0460855ef..c063e45371 100644 --- a/triedb/pathdb/history.go +++ b/triedb/pathdb/history.go @@ -250,15 +250,11 @@ type history struct { // newHistory constructs the state history object with provided state change set. func newHistory(root common.Hash, parent common.Hash, block uint64, accounts map[common.Address][]byte, storages map[common.Address]map[common.Hash][]byte, rawStorageKey bool) *history { var ( - accountList = slices.Collect(maps.Keys(accounts)) + accountList = slices.SortedFunc(maps.Keys(accounts), common.Address.Cmp) storageList = make(map[common.Address][]common.Hash) ) - slices.SortFunc(accountList, common.Address.Cmp) - for addr, slots := range storages { - slist := slices.Collect(maps.Keys(slots)) - slices.SortFunc(slist, common.Hash.Cmp) - storageList[addr] = slist + storageList[addr] = slices.SortedFunc(maps.Keys(slots), common.Hash.Cmp) } version := historyVersion if !rawStorageKey {