Skip to content

Commit e446306

Browse files
authored
Revert "core: use slices package for sorting (ethereum#27489)"
This reverts commit d7a94d9.
1 parent 2ea2fce commit e446306

File tree

8 files changed

+94
-39
lines changed

8 files changed

+94
-39
lines changed

core/blockchain.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"math/big"
2525
"runtime"
26+
"sort"
2627
"strings"
2728
"sync"
2829
"sync/atomic"
@@ -47,7 +48,6 @@ import (
4748
"github.com/ethereum/go-ethereum/params"
4849
"github.com/ethereum/go-ethereum/rlp"
4950
"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-
slices.SortFunc(blocks, func(a, b *types.Block) bool {
1019-
return a.NumberU64() < b.NumberU64()
1018+
sort.Slice(blocks, func(i, j int) bool {
1019+
return blocks[i].NumberU64() < blocks[j].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"
2728
"strings"
2829

2930
"github.com/ethereum/go-ethereum/common"
3031
"github.com/ethereum/go-ethereum/core/types"
3132
"github.com/ethereum/go-ethereum/log"
3233
"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-
slices.Sort(forksByBlock)
274-
slices.Sort(forksByTime)
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] })
275275

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

core/mkalloc.go

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

3536
"github.com/ethereum/go-ethereum/core"
3637
"github.com/ethereum/go-ethereum/rlp"
37-
"golang.org/x/exp/slices"
3838
)
3939

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

42-
func makelist(g *core.Genesis) []allocItem {
43-
items := make([]allocItem, 0, len(g.Alloc))
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))
4450
for addr, account := range g.Alloc {
4551
if len(account.Storage) > 0 || len(account.Code) > 0 || account.Nonce != 0 {
4652
panic(fmt.Sprintf("can't encode account %x", addr))
4753
}
4854
bigAddr := new(big.Int).SetBytes(addr.Bytes())
49-
items = append(items, allocItem{bigAddr, account.Balance})
55+
a = append(a, allocItem{bigAddr, account.Balance})
5056
}
51-
slices.SortFunc(items, func(a, b allocItem) bool {
52-
return a.Addr.Cmp(b.Addr) < 0
53-
})
54-
return items
57+
sort.Sort(a)
58+
return a
5559
}
5660

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

core/rawdb/accessors_chain.go

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

2627
"github.com/ethereum/go-ethereum/common"
2728
"github.com/ethereum/go-ethereum/core/types"
@@ -30,7 +31,6 @@ import (
3031
"github.com/ethereum/go-ethereum/log"
3132
"github.com/ethereum/go-ethereum/params"
3233
"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,13 +836,23 @@ 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+
839849
// ReadBadBlock retrieves the bad block with the corresponding block hash.
840850
func ReadBadBlock(db ethdb.Reader, hash common.Hash) *types.Block {
841851
blob, err := db.Get(badBlockKey)
842852
if err != nil {
843853
return nil
844854
}
845-
var badBlocks []*badBlock
855+
var badBlocks badBlockList
846856
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
847857
return nil
848858
}
@@ -861,7 +871,7 @@ func ReadAllBadBlocks(db ethdb.Reader) []*types.Block {
861871
if err != nil {
862872
return nil
863873
}
864-
var badBlocks []*badBlock
874+
var badBlocks badBlockList
865875
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
866876
return nil
867877
}
@@ -879,7 +889,7 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
879889
if err != nil {
880890
log.Warn("Failed to load old bad blocks", "error", err)
881891
}
882-
var badBlocks []*badBlock
892+
var badBlocks badBlockList
883893
if len(blob) > 0 {
884894
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
885895
log.Crit("Failed to decode old bad blocks", "error", err)
@@ -895,10 +905,7 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
895905
Header: block.Header(),
896906
Body: block.Body(),
897907
})
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-
})
908+
sort.Sort(sort.Reverse(badBlocks))
902909
if len(badBlocks) > badBlockToKeep {
903910
badBlocks = badBlocks[:badBlockToKeep]
904911
}

core/rawdb/chain_iterator_test.go

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

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

3030
func TestChainIterator(t *testing.T) {
@@ -92,11 +92,9 @@ func TestChainIterator(t *testing.T) {
9292
}
9393
}
9494
if !c.reverse {
95-
slices.Sort(numbers)
95+
sort.Ints(numbers)
9696
} else {
97-
slices.SortFunc(numbers, func(a, b int) bool {
98-
return a > b // Sort descending
99-
})
97+
sort.Sort(sort.Reverse(sort.IntSlice(numbers)))
10098
}
10199
if !reflect.DeepEqual(numbers, c.expect) {
102100
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,6 +21,7 @@ import (
2121
"fmt"
2222
"math"
2323
"math/rand"
24+
"sort"
2425
"sync"
2526
"sync/atomic"
2627
"time"
@@ -29,7 +30,6 @@ import (
2930
"github.com/ethereum/go-ethereum/core/types"
3031
"github.com/ethereum/go-ethereum/rlp"
3132
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-
slices.SortFunc(dl.accountList, common.Hash.Less)
528+
sort.Sort(hashes(dl.accountList))
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-
slices.SortFunc(storageList, common.Hash.Less)
566+
sort.Sort(hashes(storageList))
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: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"sort"
2323

2424
"github.com/ethereum/go-ethereum/common"
25-
"golang.org/x/exp/slices"
2625
)
2726

2827
// weightedIterator is a iterator with an assigned weight. It is used to prioritise
@@ -33,10 +32,18 @@ type weightedIterator struct {
3332
priority int
3433
}
3534

36-
func (it *weightedIterator) Less(other *weightedIterator) bool {
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 {
3744
// Order the iterators primarily by the account hashes
38-
hashI := it.it.Hash()
39-
hashJ := other.it.Hash()
45+
hashI := its[i].it.Hash()
46+
hashJ := its[j].it.Hash()
4047

4148
switch bytes.Compare(hashI[:], hashJ[:]) {
4249
case -1:
@@ -45,7 +52,12 @@ func (it *weightedIterator) Less(other *weightedIterator) bool {
4552
return false
4653
}
4754
// Same account/storage-slot in multiple layers, split by priority
48-
return it.priority < other.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]
4961
}
5062

5163
// fastIterator is a more optimized multi-layer iterator which maintains a
@@ -57,7 +69,7 @@ type fastIterator struct {
5769
curAccount []byte
5870
curSlot []byte
5971

60-
iterators []*weightedIterator
72+
iterators weightedIterators
6173
initiated bool
6274
account bool
6375
fail error
@@ -155,9 +167,7 @@ func (fi *fastIterator) init() {
155167
}
156168
}
157169
// Re-sort the entire list
158-
slices.SortFunc(fi.iterators, func(a, b *weightedIterator) bool {
159-
return a.Less(b)
160-
})
170+
sort.Sort(fi.iterators)
161171
fi.initiated = false
162172
}
163173

core/state/snapshot/sort.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package snapshot
18+
19+
import (
20+
"bytes"
21+
22+
"github.com/ethereum/go-ethereum/common"
23+
)
24+
25+
// hashes is a helper to implement sort.Interface.
26+
type hashes []common.Hash
27+
28+
// Len is the number of elements in the collection.
29+
func (hs hashes) Len() int { return len(hs) }
30+
31+
// Less reports whether the element with index i should sort before the element
32+
// with index j.
33+
func (hs hashes) Less(i, j int) bool { return bytes.Compare(hs[i][:], hs[j][:]) < 0 }
34+
35+
// Swap swaps the elements with indexes i and j.
36+
func (hs hashes) Swap(i, j int) { hs[i], hs[j] = hs[j], hs[i] }

0 commit comments

Comments
 (0)