From bad008f98dc0882770792742128516c92fb1c613 Mon Sep 17 00:00:00 2001 From: Jes Cok Date: Sat, 4 Jan 2025 21:40:29 +0800 Subject: [PATCH] all: simplify and clean up This patch modernizes the for-range-loop code to copy a map with "maps.Clone" and "maps.Copy", also eliminates "copyMap" and "copyFloats" functions. Also simplify "aggSort" and "sortMap" with slices and maps functions. Signed-off-by: Jes Cok --- pkg/featuregate/feature_gate.go | 37 ++++--------------- pkg/report/report.go | 20 ++-------- server/auth/jwt_test.go | 6 +-- server/proxy/grpcproxy/adapter/chan_stream.go | 5 +-- tests/framework/e2e/cluster.go | 6 +-- tools/etcd-dump-metrics/utils.go | 25 ++++--------- 6 files changed, 26 insertions(+), 73 deletions(-) diff --git a/pkg/featuregate/feature_gate.go b/pkg/featuregate/feature_gate.go index cb77017b5a7..020e07d297a 100644 --- a/pkg/featuregate/feature_gate.go +++ b/pkg/featuregate/feature_gate.go @@ -18,6 +18,7 @@ package featuregate import ( "flag" "fmt" + "maps" "sort" "strconv" "strings" @@ -171,10 +172,7 @@ func New(name string, lg *zap.Logger) *featureGate { if lg == nil { lg = zap.NewNop() } - known := map[Feature]FeatureSpec{} - for k, v := range defaultFeatures { - known[k] = v - } + known := maps.Clone(defaultFeatures) f := &featureGate{ lg: lg, @@ -216,14 +214,8 @@ func (f *featureGate) SetFromMap(m map[string]bool) error { defer f.lock.Unlock() // Copy existing state - known := map[Feature]FeatureSpec{} - for k, v := range f.known.Load().(map[Feature]FeatureSpec) { - known[k] = v - } - enabled := map[Feature]bool{} - for k, v := range f.enabled.Load().(map[Feature]bool) { - enabled[k] = v - } + known := maps.Clone(f.known.Load().(map[Feature]FeatureSpec)) + enabled := maps.Clone(f.enabled.Load().(map[Feature]bool)) for k, v := range m { k := Feature(k) @@ -279,10 +271,7 @@ func (f *featureGate) Add(features map[Feature]FeatureSpec) error { } // Copy existing state - known := map[Feature]FeatureSpec{} - for k, v := range f.known.Load().(map[Feature]FeatureSpec) { - known[k] = v - } + known := maps.Clone(f.known.Load().(map[Feature]FeatureSpec)) for name, spec := range features { if existingSpec, found := known[name]; found { @@ -335,11 +324,7 @@ func (f *featureGate) OverrideDefault(name Feature, override bool) error { // GetAll returns a copy of the map of known feature names to feature specs. func (f *featureGate) GetAll() map[Feature]FeatureSpec { - retval := map[Feature]FeatureSpec{} - for k, v := range f.known.Load().(map[Feature]FeatureSpec) { - retval[k] = v - } - return retval + return maps.Clone(f.known.Load().(map[Feature]FeatureSpec)) } // Enabled returns true if the key is enabled. If the key is not known, this call will panic. @@ -396,14 +381,8 @@ func (f *featureGate) KnownFeatures() []string { // config against potential feature gate changes before committing those changes. func (f *featureGate) DeepCopy() MutableFeatureGate { // Copy existing state. - known := map[Feature]FeatureSpec{} - for k, v := range f.known.Load().(map[Feature]FeatureSpec) { - known[k] = v - } - enabled := map[Feature]bool{} - for k, v := range f.enabled.Load().(map[Feature]bool) { - enabled[k] = v - } + known := maps.Clone(f.known.Load().(map[Feature]FeatureSpec)) + enabled := maps.Clone(f.enabled.Load().(map[Feature]bool)) // Construct a new featureGate around the copied state. // Note that specialFeatures is treated as immutable by convention, diff --git a/pkg/report/report.go b/pkg/report/report.go index 4d138f9744e..570c75d7a61 100644 --- a/pkg/report/report.go +++ b/pkg/report/report.go @@ -18,7 +18,9 @@ package report import ( "fmt" + "maps" "math" + "slices" "sort" "strings" "time" @@ -62,8 +64,8 @@ type Stats struct { func (s *Stats) copy() Stats { ss := *s - ss.ErrorDist = copyMap(ss.ErrorDist) - ss.Lats = copyFloats(ss.Lats) + ss.ErrorDist = maps.Clone(ss.ErrorDist) + ss.Lats = slices.Clone(ss.Lats) return ss } @@ -122,20 +124,6 @@ func (r *report) Stats() <-chan Stats { return donec } -func copyMap(m map[string]int) (c map[string]int) { - c = make(map[string]int, len(m)) - for k, v := range m { - c[k] = v - } - return c -} - -func copyFloats(s []float64) (c []float64) { - c = make([]float64, len(s)) - copy(c, s) - return c -} - func (r *report) String() (s string) { if len(r.stats.Lats) > 0 { s += "\nSummary:\n" diff --git a/server/auth/jwt_test.go b/server/auth/jwt_test.go index ff0e4c41989..231a86c4871 100644 --- a/server/auth/jwt_test.go +++ b/server/auth/jwt_test.go @@ -18,6 +18,7 @@ import ( "context" "errors" "fmt" + "maps" "testing" "time" @@ -117,10 +118,7 @@ func testJWTInfo(t *testing.T, opts map[string]string) { // test verify-only provider if opts["pub-key"] != "" && opts["priv-key"] != "" { t.Run("verify-only", func(t *testing.T) { - newOpts := make(map[string]string, len(opts)) - for k, v := range opts { - newOpts[k] = v - } + newOpts := maps.Clone(opts) delete(newOpts, "priv-key") verify, err := newTokenProviderJWT(lg, newOpts) if err != nil { diff --git a/server/proxy/grpcproxy/adapter/chan_stream.go b/server/proxy/grpcproxy/adapter/chan_stream.go index eaf4309c978..802c841d3d2 100644 --- a/server/proxy/grpcproxy/adapter/chan_stream.go +++ b/server/proxy/grpcproxy/adapter/chan_stream.go @@ -16,6 +16,7 @@ package adapter import ( "context" + "maps" "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -38,9 +39,7 @@ func (ss *chanServerStream) SendHeader(md metadata.MD) error { } outmd := make(map[string][]string) for _, h := range append(ss.headers, md) { - for k, v := range h { - outmd[k] = v - } + maps.Copy(outmd, h) } select { case ss.headerc <- outmd: diff --git a/tests/framework/e2e/cluster.go b/tests/framework/e2e/cluster.go index 083dcc7a077..69b450c16cb 100644 --- a/tests/framework/e2e/cluster.go +++ b/tests/framework/e2e/cluster.go @@ -19,6 +19,7 @@ import ( "errors" "flag" "fmt" + "maps" "net/url" "path" "path/filepath" @@ -633,10 +634,7 @@ func (cfg *EtcdProcessClusterConfig) EtcdServerProcessConfig(tb testing.TB, i in } args = append(args, fmt.Sprintf("--%s=%s", flag, value)) } - envVars := map[string]string{} - for key, value := range cfg.EnvVars { - envVars[key] = value - } + envVars := maps.Clone(cfg.EnvVars) var gofailPort int if cfg.GoFailEnabled { gofailPort = (i+1)*10000 + 2381 diff --git a/tools/etcd-dump-metrics/utils.go b/tools/etcd-dump-metrics/utils.go index 6a9d093b3f2..ebbcb4010c4 100644 --- a/tools/etcd-dump-metrics/utils.go +++ b/tools/etcd-dump-metrics/utils.go @@ -14,26 +14,17 @@ package main -import "sort" +import ( + "maps" + "slices" +) func aggSort(ss []string) (sorted []string) { - set := make(map[string]struct{}) - for _, s := range ss { - set[s] = struct{}{} - } - sorted = make([]string, 0, len(set)) - for k := range set { - sorted = append(sorted, k) - } - sort.Strings(sorted) - return sorted + dup := slices.Clone(ss) + slices.Sort(dup) + return slices.Compact(dup) } func sortMap(set map[string]struct{}) (sorted []string) { - sorted = make([]string, 0, len(set)) - for k := range set { - sorted = append(sorted, k) - } - sort.Strings(sorted) - return sorted + return slices.Sorted(maps.Keys(set)) }