From 1cb10eb423baa47964b0577545ebcb6df803dee5 Mon Sep 17 00:00:00 2001 From: Dan Laine Date: Fri, 16 Jun 2023 09:16:39 -0400 Subject: [PATCH 1/3] metrics, node; use slices package for sorting --- metrics/prometheus/prometheus.go | 4 ++-- metrics/resetting_timer.go | 12 +++--------- metrics/sample.go | 15 +++++---------- metrics/writer.go | 18 ++++++------------ metrics/writer_test.go | 7 ++++--- node/rpcstack.go | 4 ++-- 6 files changed, 22 insertions(+), 38 deletions(-) diff --git a/metrics/prometheus/prometheus.go b/metrics/prometheus/prometheus.go index d966fa9a8666..1627fc9db994 100644 --- a/metrics/prometheus/prometheus.go +++ b/metrics/prometheus/prometheus.go @@ -20,10 +20,10 @@ package prometheus import ( "fmt" "net/http" - "sort" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" + "golang.org/x/exp/slices" ) // Handler returns an HTTP handler which dump metrics in Prometheus format. @@ -34,7 +34,7 @@ func Handler(reg metrics.Registry) http.Handler { reg.Each(func(name string, i interface{}) { names = append(names, name) }) - sort.Strings(names) + slices.Sort(names) // Aggregate all the metrics into a Prometheus collector c := newCollector() diff --git a/metrics/resetting_timer.go b/metrics/resetting_timer.go index e5327d3bd339..924ba11052c1 100644 --- a/metrics/resetting_timer.go +++ b/metrics/resetting_timer.go @@ -2,9 +2,10 @@ package metrics import ( "math" - "sort" "sync" "time" + + "golang.org/x/exp/slices" ) // Initial slice capacity for the values stored in a ResettingTimer @@ -186,7 +187,7 @@ func (t *ResettingTimerSnapshot) Mean() float64 { } func (t *ResettingTimerSnapshot) calc(percentiles []float64) { - sort.Sort(Int64Slice(t.values)) + slices.Sort(t.values) count := len(t.values) if count > 0 { @@ -232,10 +233,3 @@ func (t *ResettingTimerSnapshot) calc(percentiles []float64) { t.calculated = true } - -// Int64Slice attaches the methods of sort.Interface to []int64, sorting in increasing order. -type Int64Slice []int64 - -func (s Int64Slice) Len() int { return len(s) } -func (s Int64Slice) Less(i, j int) bool { return s[i] < s[j] } -func (s Int64Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } diff --git a/metrics/sample.go b/metrics/sample.go index afcaa2118426..252a878f581b 100644 --- a/metrics/sample.go +++ b/metrics/sample.go @@ -3,9 +3,10 @@ package metrics import ( "math" "math/rand" - "sort" "sync" "time" + + "golang.org/x/exp/slices" ) const rescaleThreshold = time.Hour @@ -282,17 +283,17 @@ func SampleMin(values []int64) int64 { } // SamplePercentiles returns an arbitrary percentile of the slice of int64. -func SamplePercentile(values int64Slice, p float64) float64 { +func SamplePercentile(values []int64, p float64) float64 { return SamplePercentiles(values, []float64{p})[0] } // SamplePercentiles returns a slice of arbitrary percentiles of the slice of // int64. -func SamplePercentiles(values int64Slice, ps []float64) []float64 { +func SamplePercentiles(values []int64, ps []float64) []float64 { scores := make([]float64, len(ps)) size := len(values) if size > 0 { - sort.Sort(values) + slices.Sort(values) for i, p := range ps { pos := p * float64(size+1) if pos < 1.0 { @@ -633,9 +634,3 @@ func (h *expDecaySampleHeap) down(i, n int) { i = j } } - -type int64Slice []int64 - -func (p int64Slice) Len() int { return len(p) } -func (p int64Slice) Less(i, j int) bool { return p[i] < p[j] } -func (p int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } diff --git a/metrics/writer.go b/metrics/writer.go index 256fbd14c9b9..43d3252f3043 100644 --- a/metrics/writer.go +++ b/metrics/writer.go @@ -3,8 +3,9 @@ package metrics import ( "fmt" "io" - "sort" "time" + + "golang.org/x/exp/slices" ) // Write sorts writes each metric in the given registry periodically to the @@ -18,12 +19,12 @@ func Write(r Registry, d time.Duration, w io.Writer) { // WriteOnce sorts and writes metrics in the given registry to the given // io.Writer. func WriteOnce(r Registry, w io.Writer) { - var namedMetrics namedMetricSlice + var namedMetrics []namedMetric r.Each(func(name string, i interface{}) { namedMetrics = append(namedMetrics, namedMetric{name, i}) }) - sort.Sort(namedMetrics) + slices.SortFunc(namedMetrics, namedMetricLessFunc) for _, namedMetric := range namedMetrics { switch metric := namedMetric.m.(type) { case Counter: @@ -91,13 +92,6 @@ type namedMetric struct { m interface{} } -// namedMetricSlice is a slice of namedMetrics that implements sort.Interface. -type namedMetricSlice []namedMetric - -func (nms namedMetricSlice) Len() int { return len(nms) } - -func (nms namedMetricSlice) Swap(i, j int) { nms[i], nms[j] = nms[j], nms[i] } - -func (nms namedMetricSlice) Less(i, j int) bool { - return nms[i].name < nms[j].name +func namedMetricLessFunc(a, b namedMetric) bool { + return a.name < b.name } diff --git a/metrics/writer_test.go b/metrics/writer_test.go index 1aacc287121b..53b06013cd5a 100644 --- a/metrics/writer_test.go +++ b/metrics/writer_test.go @@ -1,19 +1,20 @@ package metrics import ( - "sort" "testing" + + "golang.org/x/exp/slices" ) func TestMetricsSorting(t *testing.T) { - var namedMetrics = namedMetricSlice{ + var namedMetrics = []namedMetric{ {name: "zzz"}, {name: "bbb"}, {name: "fff"}, {name: "ggg"}, } - sort.Sort(namedMetrics) + slices.SortFunc(namedMetrics, namedMetricLessFunc) for i, name := range []string{"bbb", "fff", "ggg", "zzz"} { if namedMetrics[i].name != name { t.Fail() diff --git a/node/rpcstack.go b/node/rpcstack.go index e91585a2b630..e91079d74292 100644 --- a/node/rpcstack.go +++ b/node/rpcstack.go @@ -23,7 +23,6 @@ import ( "io" "net" "net/http" - "sort" "strconv" "strings" "sync" @@ -33,6 +32,7 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rpc" "github.com/rs/cors" + "golang.org/x/exp/slices" ) // httpConfig is the JSON-RPC/HTTP configuration. @@ -182,7 +182,7 @@ func (h *httpServer) start() error { for path := range h.handlerNames { paths = append(paths, path) } - sort.Strings(paths) + slices.Sort(paths) logged := make(map[string]bool, len(paths)) for _, path := range paths { name := h.handlerNames[path] From cef420e01eb903c1ee85cbc185f3bd2621d21554 Mon Sep 17 00:00:00 2001 From: Dan Laine Date: Fri, 16 Jun 2023 15:02:00 -0400 Subject: [PATCH 2/3] metrics, node; use sort.Strings for strings --- metrics/prometheus/prometheus.go | 4 ++-- node/rpcstack.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/metrics/prometheus/prometheus.go b/metrics/prometheus/prometheus.go index 1627fc9db994..d966fa9a8666 100644 --- a/metrics/prometheus/prometheus.go +++ b/metrics/prometheus/prometheus.go @@ -20,10 +20,10 @@ package prometheus import ( "fmt" "net/http" + "sort" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" - "golang.org/x/exp/slices" ) // Handler returns an HTTP handler which dump metrics in Prometheus format. @@ -34,7 +34,7 @@ func Handler(reg metrics.Registry) http.Handler { reg.Each(func(name string, i interface{}) { names = append(names, name) }) - slices.Sort(names) + sort.Strings(names) // Aggregate all the metrics into a Prometheus collector c := newCollector() diff --git a/node/rpcstack.go b/node/rpcstack.go index e91079d74292..e91585a2b630 100644 --- a/node/rpcstack.go +++ b/node/rpcstack.go @@ -23,6 +23,7 @@ import ( "io" "net" "net/http" + "sort" "strconv" "strings" "sync" @@ -32,7 +33,6 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rpc" "github.com/rs/cors" - "golang.org/x/exp/slices" ) // httpConfig is the JSON-RPC/HTTP configuration. @@ -182,7 +182,7 @@ func (h *httpServer) start() error { for path := range h.handlerNames { paths = append(paths, path) } - slices.Sort(paths) + sort.Strings(paths) logged := make(map[string]bool, len(paths)) for _, path := range paths { name := h.handlerNames[path] From 9a3c2132159d5622eff3003a47c4ce16cd56c8a8 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 19 Jun 2023 08:49:25 +0200 Subject: [PATCH 3/3] metrics: rename function --- metrics/writer.go | 6 +++--- metrics/writer_test.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/metrics/writer.go b/metrics/writer.go index 43d3252f3043..3d60485b6b5b 100644 --- a/metrics/writer.go +++ b/metrics/writer.go @@ -24,7 +24,7 @@ func WriteOnce(r Registry, w io.Writer) { namedMetrics = append(namedMetrics, namedMetric{name, i}) }) - slices.SortFunc(namedMetrics, namedMetricLessFunc) + slices.SortFunc(namedMetrics, namedMetric.less) for _, namedMetric := range namedMetrics { switch metric := namedMetric.m.(type) { case Counter: @@ -92,6 +92,6 @@ type namedMetric struct { m interface{} } -func namedMetricLessFunc(a, b namedMetric) bool { - return a.name < b.name +func (m namedMetric) less(other namedMetric) bool { + return m.name < other.name } diff --git a/metrics/writer_test.go b/metrics/writer_test.go index 53b06013cd5a..a4c92addc9b9 100644 --- a/metrics/writer_test.go +++ b/metrics/writer_test.go @@ -14,7 +14,7 @@ func TestMetricsSorting(t *testing.T) { {name: "ggg"}, } - slices.SortFunc(namedMetrics, namedMetricLessFunc) + slices.SortFunc(namedMetrics, namedMetric.less) for i, name := range []string{"bbb", "fff", "ggg", "zzz"} { if namedMetrics[i].name != name { t.Fail()