Skip to content

Commit 767c202

Browse files
holimanfjl
andauthored
all: drop x/exp direct dependency (#30558)
This is a not-particularly-important "cleanliness" PR. It removes the last remnants of the `x/exp` package, where we used the `maps.Keys` function. The original returned the keys in a slice, but when it became 'native' the signature changed to return an iterator, so the new idiom is `slices.Collect(maps.Keys(theMap))`, unless of course the raw iterator can be used instead. In some cases, where we previously collect into slice and then sort, we can now instead do `slices.SortXX` on the iterator instead, making the code a bit more concise. This PR might be _slighly_ less optimal, because the original `x/exp` implementation allocated the slice at the correct size off the bat, which I suppose the new code won't. Putting it up for discussion. --------- Co-authored-by: Felix Lange <[email protected]>
1 parent f005d95 commit 767c202

File tree

11 files changed

+24
-36
lines changed

11 files changed

+24
-36
lines changed

cmd/devp2p/internal/ethtest/chain.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"errors"
2525
"fmt"
2626
"io"
27+
"maps"
2728
"math/big"
2829
"os"
2930
"path/filepath"
@@ -40,7 +41,6 @@ import (
4041
"github.com/ethereum/go-ethereum/eth/protocols/eth"
4142
"github.com/ethereum/go-ethereum/params"
4243
"github.com/ethereum/go-ethereum/rlp"
43-
"golang.org/x/exp/maps"
4444
)
4545

4646
// Chain is a lightweight blockchain-like store which can read a hivechain
@@ -162,8 +162,8 @@ func (c *Chain) RootAt(height int) common.Hash {
162162
// GetSender returns the address associated with account at the index in the
163163
// pre-funded accounts list.
164164
func (c *Chain) GetSender(idx int) (common.Address, uint64) {
165-
accounts := maps.Keys(c.senders)
166-
slices.SortFunc(accounts, common.Address.Cmp)
165+
accounts := slices.SortedFunc(maps.Keys(c.senders), common.Address.Cmp)
166+
167167
addr := accounts[idx]
168168
return addr, c.senders[addr].Nonce
169169
}

cmd/evm/blockrunner.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/json"
2121
"errors"
2222
"fmt"
23+
"maps"
2324
"os"
2425
"regexp"
2526
"slices"
@@ -28,7 +29,6 @@ import (
2829
"github.com/ethereum/go-ethereum/core/rawdb"
2930
"github.com/ethereum/go-ethereum/tests"
3031
"github.com/urfave/cli/v2"
31-
"golang.org/x/exp/maps"
3232
)
3333

3434
var blockTestCommand = &cli.Command{
@@ -80,8 +80,7 @@ func runBlockTest(ctx *cli.Context, fname string) ([]testResult, error) {
8080
tracer := tracerFromFlags(ctx)
8181

8282
// Pull out keys to sort and ensure tests are run in order.
83-
keys := maps.Keys(tests)
84-
slices.Sort(keys)
83+
keys := slices.Sorted(maps.Keys(tests))
8584

8685
// Run all the tests.
8786
var results []testResult

core/state/snapshot/difflayer.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package snapshot
1919
import (
2020
"encoding/binary"
2121
"fmt"
22+
"maps"
2223
"math"
2324
"math/rand"
2425
"slices"
@@ -30,7 +31,6 @@ import (
3031
"github.com/ethereum/go-ethereum/core/types"
3132
"github.com/ethereum/go-ethereum/rlp"
3233
bloomfilter "github.com/holiman/bloomfilter/v2"
33-
"golang.org/x/exp/maps"
3434
)
3535

3636
var (
@@ -431,8 +431,7 @@ func (dl *diffLayer) AccountList() []common.Hash {
431431
dl.lock.Lock()
432432
defer dl.lock.Unlock()
433433

434-
dl.accountList = maps.Keys(dl.accountData)
435-
slices.SortFunc(dl.accountList, common.Hash.Cmp)
434+
dl.accountList = slices.SortedFunc(maps.Keys(dl.accountData), common.Hash.Cmp)
436435
dl.memory += uint64(len(dl.accountList) * common.HashLength)
437436
return dl.accountList
438437
}
@@ -464,8 +463,7 @@ func (dl *diffLayer) StorageList(accountHash common.Hash) []common.Hash {
464463
dl.lock.Lock()
465464
defer dl.lock.Unlock()
466465

467-
storageList := maps.Keys(dl.storageData[accountHash])
468-
slices.SortFunc(storageList, common.Hash.Cmp)
466+
storageList := slices.SortedFunc(maps.Keys(dl.storageData[accountHash]), common.Hash.Cmp)
469467
dl.storageList[accountHash] = storageList
470468
dl.memory += uint64(len(dl.storageList)*common.HashLength + common.HashLength)
471469
return storageList

core/txpool/blobpool/evictheap.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ package blobpool
1818

1919
import (
2020
"container/heap"
21+
"maps"
2122
"math"
2223
"slices"
2324

2425
"github.com/ethereum/go-ethereum/common"
2526
"github.com/holiman/uint256"
26-
"golang.org/x/exp/maps"
2727
)
2828

2929
// evictHeap is a helper data structure to keep track of the cheapest bottleneck
@@ -54,8 +54,7 @@ func newPriceHeap(basefee *uint256.Int, blobfee *uint256.Int, index map[common.A
5454
// Populate the heap in account sort order. Not really needed in practice,
5555
// but it makes the heap initialization deterministic and less annoying to
5656
// test in unit tests.
57-
heap.addrs = maps.Keys(index)
58-
slices.SortFunc(heap.addrs, common.Address.Cmp)
57+
heap.addrs = slices.SortedFunc(maps.Keys(index), common.Address.Cmp)
5958
for i, addr := range heap.addrs {
6059
heap.index[addr] = i
6160
}

core/txpool/legacypool/legacypool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package legacypool
1919

2020
import (
2121
"errors"
22+
"maps"
2223
"math"
2324
"math/big"
2425
"slices"
@@ -40,7 +41,6 @@ import (
4041
"github.com/ethereum/go-ethereum/metrics"
4142
"github.com/ethereum/go-ethereum/params"
4243
"github.com/holiman/uint256"
43-
"golang.org/x/exp/maps"
4444
)
4545

4646
const (
@@ -1674,7 +1674,7 @@ func (as *accountSet) addTx(tx *types.Transaction) {
16741674
// reuse. The returned slice should not be changed!
16751675
func (as *accountSet) flatten() []common.Address {
16761676
if as.cache == nil {
1677-
as.cache = maps.Keys(as.accounts)
1677+
as.cache = slices.Collect(maps.Keys(as.accounts))
16781678
}
16791679
return as.cache
16801680
}

core/txpool/locals/tx_tracker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package locals
1919

2020
import (
21+
"slices"
2122
"sync"
2223
"time"
2324

@@ -28,7 +29,6 @@ import (
2829
"github.com/ethereum/go-ethereum/log"
2930
"github.com/ethereum/go-ethereum/metrics"
3031
"github.com/ethereum/go-ethereum/params"
31-
"golang.org/x/exp/slices"
3232
)
3333

3434
var (

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ require (
6565
go.uber.org/automaxprocs v1.5.2
6666
go.uber.org/goleak v1.3.0
6767
golang.org/x/crypto v0.32.0
68-
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
6968
golang.org/x/sync v0.10.0
7069
golang.org/x/sys v0.29.0
7170
golang.org/x/text v0.21.0
@@ -142,6 +141,7 @@ require (
142141
github.com/tklauser/go-sysconf v0.3.12 // indirect
143142
github.com/tklauser/numcpus v0.6.1 // indirect
144143
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
144+
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
145145
golang.org/x/mod v0.22.0 // indirect
146146
golang.org/x/net v0.34.0 // indirect
147147
gopkg.in/yaml.v2 v2.4.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
553553
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
554554
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
555555
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
556-
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ=
557-
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
556+
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=
557+
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
558558
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
559559
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
560560
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

p2p/netutil/net.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ import (
2121
"bytes"
2222
"errors"
2323
"fmt"
24+
"maps"
2425
"net"
2526
"net/netip"
2627
"slices"
2728
"strings"
28-
29-
"golang.org/x/exp/maps"
3029
)
3130

3231
var special4, special6 Netlist
@@ -324,8 +323,7 @@ func (s *DistinctNetSet) key(ip netip.Addr) netip.Prefix {
324323

325324
// String implements fmt.Stringer
326325
func (s DistinctNetSet) String() string {
327-
keys := maps.Keys(s.members)
328-
slices.SortFunc(keys, func(a, b netip.Prefix) int {
326+
keys := slices.SortedFunc(maps.Keys(s.members), func(a, b netip.Prefix) int {
329327
return strings.Compare(a.String(), b.String())
330328
})
331329

triedb/pathdb/history.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/binary"
2222
"errors"
2323
"fmt"
24+
"maps"
2425
"slices"
2526
"time"
2627

@@ -29,7 +30,6 @@ import (
2930
"github.com/ethereum/go-ethereum/crypto"
3031
"github.com/ethereum/go-ethereum/ethdb"
3132
"github.com/ethereum/go-ethereum/log"
32-
"golang.org/x/exp/maps"
3333
)
3434

3535
// State history records the state changes involved in executing a block. The
@@ -250,15 +250,11 @@ type history struct {
250250
// newHistory constructs the state history object with provided state change set.
251251
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 {
252252
var (
253-
accountList = maps.Keys(accounts)
253+
accountList = slices.SortedFunc(maps.Keys(accounts), common.Address.Cmp)
254254
storageList = make(map[common.Address][]common.Hash)
255255
)
256-
slices.SortFunc(accountList, common.Address.Cmp)
257-
258256
for addr, slots := range storages {
259-
slist := maps.Keys(slots)
260-
slices.SortFunc(slist, common.Hash.Cmp)
261-
storageList[addr] = slist
257+
storageList[addr] = slices.SortedFunc(maps.Keys(slots), common.Hash.Cmp)
262258
}
263259
version := historyVersion
264260
if !rawStorageKey {

0 commit comments

Comments
 (0)