Skip to content

Commit 154b016

Browse files
Dan Lainefjl
andauthored
core: use slices package for sorting (#27489)
Co-authored-by: Felix Lange <fjl@twurst.com>
1 parent 84b05d4 commit 154b016

File tree

8 files changed

+39
-94
lines changed

8 files changed

+39
-94
lines changed

core/blockchain.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"io"
2424
"math/big"
2525
"runtime"
26-
"sort"
2726
"strings"
2827
"sync"
2928
"sync/atomic"
@@ -48,6 +47,7 @@ import (
4847
"github.com/ethereum/go-ethereum/params"
4948
"github.com/ethereum/go-ethereum/rlp"
5049
"github.com/ethereum/go-ethereum/trie"
50+
"golang.org/x/exp/slices"
5151
)
5252

5353
var (
@@ -1015,8 +1015,8 @@ func (bc *BlockChain) procFutureBlocks() {
10151015
}
10161016
}
10171017
if len(blocks) > 0 {
1018-
sort.Slice(blocks, func(i, j int) bool {
1019-
return blocks[i].NumberU64() < blocks[j].NumberU64()
1018+
slices.SortFunc(blocks, func(a, b *types.Block) bool {
1019+
return a.NumberU64() < b.NumberU64()
10201020
})
10211021
// Insert one by one as chain insertion needs contiguous ancestry between blocks
10221022
for i := range blocks {

core/forkid/forkid.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ import (
2424
"math"
2525
"math/big"
2626
"reflect"
27-
"sort"
2827
"strings"
2928

3029
"github.com/ethereum/go-ethereum/common"
3130
"github.com/ethereum/go-ethereum/core/types"
3231
"github.com/ethereum/go-ethereum/log"
3332
"github.com/ethereum/go-ethereum/params"
33+
"golang.org/x/exp/slices"
3434
)
3535

3636
var (
@@ -270,8 +270,8 @@ func gatherForks(config *params.ChainConfig) ([]uint64, []uint64) {
270270
}
271271
}
272272
}
273-
sort.Slice(forksByBlock, func(i, j int) bool { return forksByBlock[i] < forksByBlock[j] })
274-
sort.Slice(forksByTime, func(i, j int) bool { return forksByTime[i] < forksByTime[j] })
273+
slices.Sort(forksByBlock)
274+
slices.Sort(forksByTime)
275275

276276
// Deduplicate fork identifiers applying multiple forks
277277
for i := 1; i < len(forksByBlock); i++ {

core/mkalloc.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,28 @@ import (
3030
"fmt"
3131
"math/big"
3232
"os"
33-
"sort"
3433
"strconv"
3534

3635
"github.com/ethereum/go-ethereum/core"
3736
"github.com/ethereum/go-ethereum/rlp"
37+
"golang.org/x/exp/slices"
3838
)
3939

4040
type allocItem struct{ Addr, Balance *big.Int }
4141

42-
type allocList []allocItem
43-
44-
func (a allocList) Len() int { return len(a) }
45-
func (a allocList) Less(i, j int) bool { return a[i].Addr.Cmp(a[j].Addr) < 0 }
46-
func (a allocList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
47-
48-
func makelist(g *core.Genesis) allocList {
49-
a := make(allocList, 0, len(g.Alloc))
42+
func makelist(g *core.Genesis) []allocItem {
43+
items := make([]allocItem, 0, len(g.Alloc))
5044
for addr, account := range g.Alloc {
5145
if len(account.Storage) > 0 || len(account.Code) > 0 || account.Nonce != 0 {
5246
panic(fmt.Sprintf("can't encode account %x", addr))
5347
}
5448
bigAddr := new(big.Int).SetBytes(addr.Bytes())
55-
a = append(a, allocItem{bigAddr, account.Balance})
49+
items = append(items, allocItem{bigAddr, account.Balance})
5650
}
57-
sort.Sort(a)
58-
return a
51+
slices.SortFunc(items, func(a, b allocItem) bool {
52+
return a.Addr.Cmp(b.Addr) < 0
53+
})
54+
return items
5955
}
6056

6157
func makealloc(g *core.Genesis) string {

core/rawdb/accessors_chain.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"errors"
2323
"fmt"
2424
"math/big"
25-
"sort"
2625

2726
"github.com/ethereum/go-ethereum/common"
2827
"github.com/ethereum/go-ethereum/core/types"
@@ -31,6 +30,7 @@ import (
3130
"github.com/ethereum/go-ethereum/log"
3231
"github.com/ethereum/go-ethereum/params"
3332
"github.com/ethereum/go-ethereum/rlp"
33+
"golang.org/x/exp/slices"
3434
)
3535

3636
// ReadCanonicalHash retrieves the hash assigned to a canonical block number.
@@ -836,23 +836,13 @@ type badBlock struct {
836836
Body *types.Body
837837
}
838838

839-
// badBlockList implements the sort interface to allow sorting a list of
840-
// bad blocks by their number in the reverse order.
841-
type badBlockList []*badBlock
842-
843-
func (s badBlockList) Len() int { return len(s) }
844-
func (s badBlockList) Less(i, j int) bool {
845-
return s[i].Header.Number.Uint64() < s[j].Header.Number.Uint64()
846-
}
847-
func (s badBlockList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
848-
849839
// ReadBadBlock retrieves the bad block with the corresponding block hash.
850840
func ReadBadBlock(db ethdb.Reader, hash common.Hash) *types.Block {
851841
blob, err := db.Get(badBlockKey)
852842
if err != nil {
853843
return nil
854844
}
855-
var badBlocks badBlockList
845+
var badBlocks []*badBlock
856846
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
857847
return nil
858848
}
@@ -871,7 +861,7 @@ func ReadAllBadBlocks(db ethdb.Reader) []*types.Block {
871861
if err != nil {
872862
return nil
873863
}
874-
var badBlocks badBlockList
864+
var badBlocks []*badBlock
875865
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
876866
return nil
877867
}
@@ -889,7 +879,7 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
889879
if err != nil {
890880
log.Warn("Failed to load old bad blocks", "error", err)
891881
}
892-
var badBlocks badBlockList
882+
var badBlocks []*badBlock
893883
if len(blob) > 0 {
894884
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
895885
log.Crit("Failed to decode old bad blocks", "error", err)
@@ -905,7 +895,10 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
905895
Header: block.Header(),
906896
Body: block.Body(),
907897
})
908-
sort.Sort(sort.Reverse(badBlocks))
898+
slices.SortFunc(badBlocks, func(a, b *badBlock) bool {
899+
// Note: sorting in descending number order.
900+
return a.Header.Number.Uint64() >= b.Header.Number.Uint64()
901+
})
909902
if len(badBlocks) > badBlockToKeep {
910903
badBlocks = badBlocks[:badBlockToKeep]
911904
}

core/rawdb/chain_iterator_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ package rawdb
1919
import (
2020
"math/big"
2121
"reflect"
22-
"sort"
2322
"sync"
2423
"testing"
2524

2625
"github.com/ethereum/go-ethereum/common"
2726
"github.com/ethereum/go-ethereum/core/types"
27+
"golang.org/x/exp/slices"
2828
)
2929

3030
func TestChainIterator(t *testing.T) {
@@ -92,9 +92,11 @@ func TestChainIterator(t *testing.T) {
9292
}
9393
}
9494
if !c.reverse {
95-
sort.Ints(numbers)
95+
slices.Sort(numbers)
9696
} else {
97-
sort.Sort(sort.Reverse(sort.IntSlice(numbers)))
97+
slices.SortFunc(numbers, func(a, b int) bool {
98+
return a > b // Sort descending
99+
})
98100
}
99101
if !reflect.DeepEqual(numbers, c.expect) {
100102
t.Fatalf("Case %d failed, visit element mismatch, want %v, got %v", i, c.expect, numbers)

core/state/snapshot/difflayer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"math"
2323
"math/rand"
24-
"sort"
2524
"sync"
2625
"sync/atomic"
2726
"time"
@@ -30,6 +29,7 @@ import (
3029
"github.com/ethereum/go-ethereum/core/types"
3130
"github.com/ethereum/go-ethereum/rlp"
3231
bloomfilter "github.com/holiman/bloomfilter/v2"
32+
"golang.org/x/exp/slices"
3333
)
3434

3535
var (
@@ -525,7 +525,7 @@ func (dl *diffLayer) AccountList() []common.Hash {
525525
dl.accountList = append(dl.accountList, hash)
526526
}
527527
}
528-
sort.Sort(hashes(dl.accountList))
528+
slices.SortFunc(dl.accountList, common.Hash.Less)
529529
dl.memory += uint64(len(dl.accountList) * common.HashLength)
530530
return dl.accountList
531531
}
@@ -563,7 +563,7 @@ func (dl *diffLayer) StorageList(accountHash common.Hash) ([]common.Hash, bool)
563563
for k := range storageMap {
564564
storageList = append(storageList, k)
565565
}
566-
sort.Sort(hashes(storageList))
566+
slices.SortFunc(storageList, common.Hash.Less)
567567
dl.storageList[accountHash] = storageList
568568
dl.memory += uint64(len(dl.storageList)*common.HashLength + common.HashLength)
569569
return storageList, destructed

core/state/snapshot/iterator_fast.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"sort"
2323

2424
"github.com/ethereum/go-ethereum/common"
25+
"golang.org/x/exp/slices"
2526
)
2627

2728
// weightedIterator is a iterator with an assigned weight. It is used to prioritise
@@ -32,18 +33,10 @@ type weightedIterator struct {
3233
priority int
3334
}
3435

35-
// weightedIterators is a set of iterators implementing the sort.Interface.
36-
type weightedIterators []*weightedIterator
37-
38-
// Len implements sort.Interface, returning the number of active iterators.
39-
func (its weightedIterators) Len() int { return len(its) }
40-
41-
// Less implements sort.Interface, returning which of two iterators in the stack
42-
// is before the other.
43-
func (its weightedIterators) Less(i, j int) bool {
36+
func (it *weightedIterator) Less(other *weightedIterator) bool {
4437
// Order the iterators primarily by the account hashes
45-
hashI := its[i].it.Hash()
46-
hashJ := its[j].it.Hash()
38+
hashI := it.it.Hash()
39+
hashJ := other.it.Hash()
4740

4841
switch bytes.Compare(hashI[:], hashJ[:]) {
4942
case -1:
@@ -52,12 +45,7 @@ func (its weightedIterators) Less(i, j int) bool {
5245
return false
5346
}
5447
// Same account/storage-slot in multiple layers, split by priority
55-
return its[i].priority < its[j].priority
56-
}
57-
58-
// Swap implements sort.Interface, swapping two entries in the iterator stack.
59-
func (its weightedIterators) Swap(i, j int) {
60-
its[i], its[j] = its[j], its[i]
48+
return it.priority < other.priority
6149
}
6250

6351
// fastIterator is a more optimized multi-layer iterator which maintains a
@@ -69,7 +57,7 @@ type fastIterator struct {
6957
curAccount []byte
7058
curSlot []byte
7159

72-
iterators weightedIterators
60+
iterators []*weightedIterator
7361
initiated bool
7462
account bool
7563
fail error
@@ -167,7 +155,9 @@ func (fi *fastIterator) init() {
167155
}
168156
}
169157
// Re-sort the entire list
170-
sort.Sort(fi.iterators)
158+
slices.SortFunc(fi.iterators, func(a, b *weightedIterator) bool {
159+
return a.Less(b)
160+
})
171161
fi.initiated = false
172162
}
173163

core/state/snapshot/sort.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)