core: use slices package for sorting (#27489)

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
Dan Laine
2023-06-20 11:58:47 +02:00
committed by GitHub
co-authored by Felix Lange
parent 84b05d4f34
commit 154b016b6c
8 changed files with 39 additions and 94 deletions
+3 -3
View File
@@ -21,7 +21,6 @@ import (
"fmt"
"math"
"math/rand"
"sort"
"sync"
"sync/atomic"
"time"
@@ -30,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
bloomfilter "github.com/holiman/bloomfilter/v2"
"golang.org/x/exp/slices"
)
var (
@@ -525,7 +525,7 @@ func (dl *diffLayer) AccountList() []common.Hash {
dl.accountList = append(dl.accountList, hash)
}
}
sort.Sort(hashes(dl.accountList))
slices.SortFunc(dl.accountList, common.Hash.Less)
dl.memory += uint64(len(dl.accountList) * common.HashLength)
return dl.accountList
}
@@ -563,7 +563,7 @@ func (dl *diffLayer) StorageList(accountHash common.Hash) ([]common.Hash, bool)
for k := range storageMap {
storageList = append(storageList, k)
}
sort.Sort(hashes(storageList))
slices.SortFunc(storageList, common.Hash.Less)
dl.storageList[accountHash] = storageList
dl.memory += uint64(len(dl.storageList)*common.HashLength + common.HashLength)
return storageList, destructed
+9 -19
View File
@@ -22,6 +22,7 @@ import (
"sort"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/exp/slices"
)
// weightedIterator is a iterator with an assigned weight. It is used to prioritise
@@ -32,18 +33,10 @@ type weightedIterator struct {
priority int
}
// weightedIterators is a set of iterators implementing the sort.Interface.
type weightedIterators []*weightedIterator
// Len implements sort.Interface, returning the number of active iterators.
func (its weightedIterators) Len() int { return len(its) }
// Less implements sort.Interface, returning which of two iterators in the stack
// is before the other.
func (its weightedIterators) Less(i, j int) bool {
func (it *weightedIterator) Less(other *weightedIterator) bool {
// Order the iterators primarily by the account hashes
hashI := its[i].it.Hash()
hashJ := its[j].it.Hash()
hashI := it.it.Hash()
hashJ := other.it.Hash()
switch bytes.Compare(hashI[:], hashJ[:]) {
case -1:
@@ -52,12 +45,7 @@ func (its weightedIterators) Less(i, j int) bool {
return false
}
// Same account/storage-slot in multiple layers, split by priority
return its[i].priority < its[j].priority
}
// Swap implements sort.Interface, swapping two entries in the iterator stack.
func (its weightedIterators) Swap(i, j int) {
its[i], its[j] = its[j], its[i]
return it.priority < other.priority
}
// fastIterator is a more optimized multi-layer iterator which maintains a
@@ -69,7 +57,7 @@ type fastIterator struct {
curAccount []byte
curSlot []byte
iterators weightedIterators
iterators []*weightedIterator
initiated bool
account bool
fail error
@@ -167,7 +155,9 @@ func (fi *fastIterator) init() {
}
}
// Re-sort the entire list
sort.Sort(fi.iterators)
slices.SortFunc(fi.iterators, func(a, b *weightedIterator) bool {
return a.Less(b)
})
fi.initiated = false
}
-36
View File
@@ -1,36 +0,0 @@
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package snapshot
import (
"bytes"
"github.com/ethereum/go-ethereum/common"
)
// hashes is a helper to implement sort.Interface.
type hashes []common.Hash
// Len is the number of elements in the collection.
func (hs hashes) Len() int { return len(hs) }
// Less reports whether the element with index i should sort before the element
// with index j.
func (hs hashes) Less(i, j int) bool { return bytes.Compare(hs[i][:], hs[j][:]) < 0 }
// Swap swaps the elements with indexes i and j.
func (hs hashes) Swap(i, j int) { hs[i], hs[j] = hs[j], hs[i] }