Skip to content

Commit 051b1a1

Browse files
Dan Lainefjl
authored andcommitted
internal/ethapi, les: use slices package for sorting (ethereum#27492)
Co-authored-by: Felix Lange <fjl@twurst.com>
1 parent cd00ee3 commit 051b1a1

File tree

3 files changed

+22
-56
lines changed

3 files changed

+22
-56
lines changed

internal/ethapi/api_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
package ethapi
1818

1919
import (
20-
"bytes"
2120
"context"
2221
"crypto/ecdsa"
2322
"encoding/json"
2423
"errors"
2524
"hash"
2625
"math/big"
2726
"reflect"
28-
"sort"
2927
"testing"
3028
"time"
3129

@@ -48,6 +46,7 @@ import (
4846
"github.com/ethereum/go-ethereum/rpc"
4947
"github.com/stretchr/testify/require"
5048
"golang.org/x/crypto/sha3"
49+
"golang.org/x/exp/slices"
5150
)
5251

5352
func TestTransaction_RoundTripRpcJSON(t *testing.T) {
@@ -649,19 +648,13 @@ type Account struct {
649648
addr common.Address
650649
}
651650

652-
type Accounts []Account
653-
654-
func (a Accounts) Len() int { return len(a) }
655-
func (a Accounts) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
656-
func (a Accounts) Less(i, j int) bool { return bytes.Compare(a[i].addr.Bytes(), a[j].addr.Bytes()) < 0 }
657-
658-
func newAccounts(n int) (accounts Accounts) {
651+
func newAccounts(n int) (accounts []Account) {
659652
for i := 0; i < n; i++ {
660653
key, _ := crypto.GenerateKey()
661654
addr := crypto.PubkeyToAddress(key.PublicKey)
662655
accounts = append(accounts, Account{key: key, addr: addr})
663656
}
664-
sort.Sort(accounts)
657+
slices.SortFunc(accounts, func(a, b Account) bool { return a.addr.Less(b.addr) })
665658
return accounts
666659
}
667660

les/servingqueue.go

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package les
1818

1919
import (
20-
"sort"
2120
"sync"
2221
"sync/atomic"
2322

2423
"github.com/ethereum/go-ethereum/common/mclock"
2524
"github.com/ethereum/go-ethereum/common/prque"
25+
"golang.org/x/exp/slices"
2626
)
2727

2828
// servingQueue allows running tasks in a limited number of threads and puts the
@@ -180,35 +180,19 @@ func (sq *servingQueue) threadController() {
180180
}
181181
}
182182

183-
type (
184-
// peerTasks lists the tasks received from a given peer when selecting peers to freeze
185-
peerTasks struct {
186-
peer *clientPeer
187-
list []*servingTask
188-
sumTime uint64
189-
priority float64
190-
}
191-
// peerList is a sortable list of peerTasks
192-
peerList []*peerTasks
193-
)
194-
195-
func (l peerList) Len() int {
196-
return len(l)
197-
}
198-
199-
func (l peerList) Less(i, j int) bool {
200-
return l[i].priority < l[j].priority
201-
}
202-
203-
func (l peerList) Swap(i, j int) {
204-
l[i], l[j] = l[j], l[i]
183+
// peerTasks lists the tasks received from a given peer when selecting peers to freeze
184+
type peerTasks struct {
185+
peer *clientPeer
186+
list []*servingTask
187+
sumTime uint64
188+
priority float64
205189
}
206190

207191
// freezePeers selects the peers with the worst priority queued tasks and freezes
208192
// them until burstTime goes under burstDropLimit or all peers are frozen
209193
func (sq *servingQueue) freezePeers() {
210194
peerMap := make(map[*clientPeer]*peerTasks)
211-
var peerList peerList
195+
var peerList []*peerTasks
212196
if sq.best != nil {
213197
sq.queue.Push(sq.best, sq.best.priority)
214198
}
@@ -231,7 +215,9 @@ func (sq *servingQueue) freezePeers() {
231215
tasks.list = append(tasks.list, task)
232216
tasks.sumTime += task.expTime
233217
}
234-
sort.Sort(peerList)
218+
slices.SortFunc(peerList, func(a, b *peerTasks) bool {
219+
return a.priority < b.priority
220+
})
235221
drop := true
236222
for _, tasks := range peerList {
237223
if drop {

les/utils/limiter.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
package utils
1818

1919
import (
20-
"sort"
2120
"sync"
2221

2322
"github.com/ethereum/go-ethereum/p2p/enode"
23+
"golang.org/x/exp/slices"
2424
)
2525

2626
const maxSelectionWeight = 1000000000 // maximum selection weight of each individual node/address group
@@ -340,24 +340,9 @@ func (l *Limiter) Stop() {
340340
l.cond.Signal()
341341
}
342342

343-
type (
344-
dropList []dropListItem
345-
dropListItem struct {
346-
nq *nodeQueue
347-
priority float64
348-
}
349-
)
350-
351-
func (l dropList) Len() int {
352-
return len(l)
353-
}
354-
355-
func (l dropList) Less(i, j int) bool {
356-
return l[i].priority < l[j].priority
357-
}
358-
359-
func (l dropList) Swap(i, j int) {
360-
l[i], l[j] = l[j], l[i]
343+
type dropListItem struct {
344+
nq *nodeQueue
345+
priority float64
361346
}
362347

363348
// dropRequests selects the nodes with the highest queued request cost to selection
@@ -366,7 +351,7 @@ func (l dropList) Swap(i, j int) {
366351
func (l *Limiter) dropRequests() {
367352
var (
368353
sumValue float64
369-
list dropList
354+
list []dropListItem
370355
)
371356
for _, nq := range l.nodes {
372357
sumValue += nq.value
@@ -384,7 +369,9 @@ func (l *Limiter) dropRequests() {
384369
priority: w / float64(nq.sumCost),
385370
})
386371
}
387-
sort.Sort(list)
372+
slices.SortFunc(list, func(a, b dropListItem) bool {
373+
return a.priority < b.priority
374+
})
388375
for _, item := range list {
389376
for _, request := range item.nq.queue {
390377
close(request.process)

0 commit comments

Comments
 (0)