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 "copyFloats" with
"slices.Clone".

Also simplify "aggSort" and "sortMap" with slices and maps functions.

Signed-off-by: Jes Cok <xigua67damn@gmail.com>
This commit is contained in:
Jes Cok
2025-01-08 09:08:25 +08:00
parent 70a172696d
commit 2415c826c2
6 changed files with 26 additions and 58 deletions

View File

@ -18,6 +18,7 @@ package featuregate
import ( import (
"flag" "flag"
"fmt" "fmt"
"maps"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -171,10 +172,7 @@ func New(name string, lg *zap.Logger) *featureGate {
if lg == nil { if lg == nil {
lg = zap.NewNop() lg = zap.NewNop()
} }
known := map[Feature]FeatureSpec{} known := maps.Clone(defaultFeatures)
for k, v := range defaultFeatures {
known[k] = v
}
f := &featureGate{ f := &featureGate{
lg: lg, lg: lg,
@ -217,13 +215,9 @@ func (f *featureGate) SetFromMap(m map[string]bool) error {
// Copy existing state // Copy existing state
known := map[Feature]FeatureSpec{} known := map[Feature]FeatureSpec{}
for k, v := range f.known.Load().(map[Feature]FeatureSpec) { maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))
known[k] = v
}
enabled := map[Feature]bool{} enabled := map[Feature]bool{}
for k, v := range f.enabled.Load().(map[Feature]bool) { maps.Copy(enabled, f.enabled.Load().(map[Feature]bool))
enabled[k] = v
}
for k, v := range m { for k, v := range m {
k := Feature(k) k := Feature(k)
@ -280,9 +274,7 @@ func (f *featureGate) Add(features map[Feature]FeatureSpec) error {
// Copy existing state // Copy existing state
known := map[Feature]FeatureSpec{} known := map[Feature]FeatureSpec{}
for k, v := range f.known.Load().(map[Feature]FeatureSpec) { maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))
known[k] = v
}
for name, spec := range features { for name, spec := range features {
if existingSpec, found := known[name]; found { if existingSpec, found := known[name]; found {
@ -336,9 +328,7 @@ func (f *featureGate) OverrideDefault(name Feature, override bool) error {
// GetAll returns a copy of the map of known feature names to feature specs. // GetAll returns a copy of the map of known feature names to feature specs.
func (f *featureGate) GetAll() map[Feature]FeatureSpec { func (f *featureGate) GetAll() map[Feature]FeatureSpec {
retval := map[Feature]FeatureSpec{} retval := map[Feature]FeatureSpec{}
for k, v := range f.known.Load().(map[Feature]FeatureSpec) { maps.Copy(retval, f.known.Load().(map[Feature]FeatureSpec))
retval[k] = v
}
return retval return retval
} }
@ -397,13 +387,9 @@ func (f *featureGate) KnownFeatures() []string {
func (f *featureGate) DeepCopy() MutableFeatureGate { func (f *featureGate) DeepCopy() MutableFeatureGate {
// Copy existing state. // Copy existing state.
known := map[Feature]FeatureSpec{} known := map[Feature]FeatureSpec{}
for k, v := range f.known.Load().(map[Feature]FeatureSpec) { maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))
known[k] = v
}
enabled := map[Feature]bool{} enabled := map[Feature]bool{}
for k, v := range f.enabled.Load().(map[Feature]bool) { maps.Copy(enabled, f.enabled.Load().(map[Feature]bool))
enabled[k] = v
}
// Construct a new featureGate around the copied state. // Construct a new featureGate around the copied state.
// Note that specialFeatures is treated as immutable by convention, // Note that specialFeatures is treated as immutable by convention,

View File

@ -18,7 +18,9 @@ package report
import ( import (
"fmt" "fmt"
"maps"
"math" "math"
"slices"
"sort" "sort"
"strings" "strings"
"time" "time"
@ -63,7 +65,7 @@ type Stats struct {
func (s *Stats) copy() Stats { func (s *Stats) copy() Stats {
ss := *s ss := *s
ss.ErrorDist = copyMap(ss.ErrorDist) ss.ErrorDist = copyMap(ss.ErrorDist)
ss.Lats = copyFloats(ss.Lats) ss.Lats = slices.Clone(ss.Lats)
return ss return ss
} }
@ -124,15 +126,7 @@ func (r *report) Stats() <-chan Stats {
func copyMap(m map[string]int) (c map[string]int) { func copyMap(m map[string]int) (c map[string]int) {
c = make(map[string]int, len(m)) c = make(map[string]int, len(m))
for k, v := range m { maps.Copy(c, m)
c[k] = v
}
return c
}
func copyFloats(s []float64) (c []float64) {
c = make([]float64, len(s))
copy(c, s)
return c return c
} }

View File

@ -18,6 +18,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"maps"
"testing" "testing"
"time" "time"
@ -118,9 +119,7 @@ func testJWTInfo(t *testing.T, opts map[string]string) {
if opts["pub-key"] != "" && opts["priv-key"] != "" { if opts["pub-key"] != "" && opts["priv-key"] != "" {
t.Run("verify-only", func(t *testing.T) { t.Run("verify-only", func(t *testing.T) {
newOpts := make(map[string]string, len(opts)) newOpts := make(map[string]string, len(opts))
for k, v := range opts { maps.Copy(newOpts, opts)
newOpts[k] = v
}
delete(newOpts, "priv-key") delete(newOpts, "priv-key")
verify, err := newTokenProviderJWT(lg, newOpts) verify, err := newTokenProviderJWT(lg, newOpts)
if err != nil { if err != nil {

View File

@ -16,6 +16,7 @@ package adapter
import ( import (
"context" "context"
"maps"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
@ -38,9 +39,7 @@ func (ss *chanServerStream) SendHeader(md metadata.MD) error {
} }
outmd := make(map[string][]string) outmd := make(map[string][]string)
for _, h := range append(ss.headers, md) { for _, h := range append(ss.headers, md) {
for k, v := range h { maps.Copy(outmd, h)
outmd[k] = v
}
} }
select { select {
case ss.headerc <- outmd: case ss.headerc <- outmd:

View File

@ -19,6 +19,7 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"maps"
"net/url" "net/url"
"path" "path"
"path/filepath" "path/filepath"
@ -634,9 +635,7 @@ func (cfg *EtcdProcessClusterConfig) EtcdServerProcessConfig(tb testing.TB, i in
args = append(args, fmt.Sprintf("--%s=%s", flag, value)) args = append(args, fmt.Sprintf("--%s=%s", flag, value))
} }
envVars := map[string]string{} envVars := map[string]string{}
for key, value := range cfg.EnvVars { maps.Copy(envVars, cfg.EnvVars)
envVars[key] = value
}
var gofailPort int var gofailPort int
if cfg.GoFailEnabled { if cfg.GoFailEnabled {
gofailPort = (i+1)*10000 + 2381 gofailPort = (i+1)*10000 + 2381

View File

@ -14,26 +14,17 @@
package main package main
import "sort" import (
"maps"
"slices"
)
func aggSort(ss []string) (sorted []string) { func aggSort(ss []string) (sorted []string) {
set := make(map[string]struct{}) dup := slices.Clone(ss)
for _, s := range ss { slices.Sort(dup)
set[s] = struct{}{} return slices.Compact(dup)
}
sorted = make([]string, 0, len(set))
for k := range set {
sorted = append(sorted, k)
}
sort.Strings(sorted)
return sorted
} }
func sortMap(set map[string]struct{}) (sorted []string) { func sortMap(set map[string]struct{}) (sorted []string) {
sorted = make([]string, 0, len(set)) return slices.Sorted(maps.Keys(set))
for k := range set {
sorted = append(sorted, k)
}
sort.Strings(sorted)
return sorted
} }