Skip to content

Commit

Permalink
all: simplify and clean up
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
callthingsoff committed Jan 4, 2025
1 parent fce823a commit bad008f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 73 deletions.
37 changes: 8 additions & 29 deletions pkg/featuregate/feature_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package featuregate
import (
"flag"
"fmt"
"maps"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 4 additions & 16 deletions pkg/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package report

import (
"fmt"
"maps"
"math"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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"
Expand Down
6 changes: 2 additions & 4 deletions server/auth/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"maps"
"testing"
"time"

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions server/proxy/grpcproxy/adapter/chan_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package adapter

import (
"context"
"maps"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand All @@ -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)

Check warning on line 42 in server/proxy/grpcproxy/adapter/chan_stream.go

View check run for this annotation

Codecov / codecov/patch

server/proxy/grpcproxy/adapter/chan_stream.go#L42

Added line #L42 was not covered by tests
}
select {
case ss.headerc <- outmd:
Expand Down
6 changes: 2 additions & 4 deletions tests/framework/e2e/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"flag"
"fmt"
"maps"
"net/url"
"path"
"path/filepath"
Expand Down Expand Up @@ -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
Expand Down
25 changes: 8 additions & 17 deletions tools/etcd-dump-metrics/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit bad008f

Please sign in to comment.