Skip to content

Commit e524043

Browse files
gzliudanfjl
andcommitted
metrics: use slices package for sorting (ethereum#27493 ethereum#27909)
Co-authored-by: Felix Lange <fjl@twurst.com>
1 parent 7d40ca6 commit e524043

6 files changed

Lines changed: 22 additions & 35 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ require (
5555
github.com/mattn/go-isatty v0.0.17
5656
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
5757
github.com/urfave/cli/v2 v2.27.5
58+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
5859
gopkg.in/natefinch/lumberjack.v2 v2.2.1
5960
)
6061

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPh
218218
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
219219
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
220220
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
221+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
222+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
221223
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
222224
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
223225
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

metrics/resetting_timer.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package metrics
22

33
import (
44
"math"
5-
"sort"
65
"sync"
76
"time"
7+
8+
"golang.org/x/exp/slices"
89
)
910

1011
// Initial slice capacity for the values stored in a ResettingTimer
@@ -182,7 +183,7 @@ func (t *ResettingTimerSnapshot) Mean() float64 {
182183
}
183184

184185
func (t *ResettingTimerSnapshot) calc(percentiles []float64) {
185-
sort.Sort(Int64Slice(t.values))
186+
slices.Sort(t.values)
186187

187188
count := len(t.values)
188189
if count > 0 {
@@ -228,10 +229,3 @@ func (t *ResettingTimerSnapshot) calc(percentiles []float64) {
228229

229230
t.calculated = true
230231
}
231-
232-
// Int64Slice attaches the methods of sort.Interface to []int64, sorting in increasing order.
233-
type Int64Slice []int64
234-
235-
func (s Int64Slice) Len() int { return len(s) }
236-
func (s Int64Slice) Less(i, j int) bool { return s[i] < s[j] }
237-
func (s Int64Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

metrics/sample.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package metrics
33
import (
44
"math"
55
"math/rand"
6-
"sort"
76
"sync"
87
"time"
8+
9+
"golang.org/x/exp/slices"
910
)
1011

1112
const rescaleThreshold = time.Hour
@@ -282,17 +283,17 @@ func SampleMin(values []int64) int64 {
282283
}
283284

284285
// SamplePercentiles returns an arbitrary percentile of the slice of int64.
285-
func SamplePercentile(values int64Slice, p float64) float64 {
286+
func SamplePercentile(values []int64, p float64) float64 {
286287
return SamplePercentiles(values, []float64{p})[0]
287288
}
288289

289290
// SamplePercentiles returns a slice of arbitrary percentiles of the slice of
290291
// int64.
291-
func SamplePercentiles(values int64Slice, ps []float64) []float64 {
292+
func SamplePercentiles(values []int64, ps []float64) []float64 {
292293
scores := make([]float64, len(ps))
293294
size := len(values)
294295
if size > 0 {
295-
sort.Sort(values)
296+
slices.Sort(values)
296297
for i, p := range ps {
297298
pos := p * float64(size+1)
298299
if pos < 1.0 {
@@ -633,9 +634,3 @@ func (h *expDecaySampleHeap) down(i, n int) {
633634
i = j
634635
}
635636
}
636-
637-
type int64Slice []int64
638-
639-
func (p int64Slice) Len() int { return len(p) }
640-
func (p int64Slice) Less(i, j int) bool { return p[i] < p[j] }
641-
func (p int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

metrics/writer.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package metrics
33
import (
44
"fmt"
55
"io"
6-
"sort"
6+
"strings"
77
"time"
8+
9+
"golang.org/x/exp/slices"
810
)
911

1012
// Write sorts writes each metric in the given registry periodically to the
@@ -18,12 +20,11 @@ func Write(r Registry, d time.Duration, w io.Writer) {
1820
// WriteOnce sorts and writes metrics in the given registry to the given
1921
// io.Writer.
2022
func WriteOnce(r Registry, w io.Writer) {
21-
var namedMetrics namedMetricSlice
23+
var namedMetrics []namedMetric
2224
r.Each(func(name string, i interface{}) {
2325
namedMetrics = append(namedMetrics, namedMetric{name, i})
2426
})
25-
26-
sort.Sort(namedMetrics)
27+
slices.SortFunc(namedMetrics, namedMetric.cmp)
2728
for _, namedMetric := range namedMetrics {
2829
switch metric := namedMetric.m.(type) {
2930
case Counter:
@@ -91,13 +92,6 @@ type namedMetric struct {
9192
m interface{}
9293
}
9394

94-
// namedMetricSlice is a slice of namedMetrics that implements sort.Interface.
95-
type namedMetricSlice []namedMetric
96-
97-
func (nms namedMetricSlice) Len() int { return len(nms) }
98-
99-
func (nms namedMetricSlice) Swap(i, j int) { nms[i], nms[j] = nms[j], nms[i] }
100-
101-
func (nms namedMetricSlice) Less(i, j int) bool {
102-
return nms[i].name < nms[j].name
95+
func (m namedMetric) cmp(other namedMetric) int {
96+
return strings.Compare(m.name, other.name)
10397
}

metrics/writer_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package metrics
22

33
import (
4-
"sort"
54
"testing"
5+
6+
"golang.org/x/exp/slices"
67
)
78

89
func TestMetricsSorting(t *testing.T) {
9-
var namedMetrics = namedMetricSlice{
10+
var namedMetrics = []namedMetric{
1011
{name: "zzz"},
1112
{name: "bbb"},
1213
{name: "fff"},
1314
{name: "ggg"},
1415
}
1516

16-
sort.Sort(namedMetrics)
17+
slices.SortFunc(namedMetrics, namedMetric.cmp)
1718
for i, name := range []string{"bbb", "fff", "ggg", "zzz"} {
1819
if namedMetrics[i].name != name {
1920
t.Fail()

0 commit comments

Comments
 (0)