From f4bde686949396292e3afd79f92c2149a7117839 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 12 Jun 2023 18:19:50 +0200 Subject: [PATCH 1/2] replace some basic uses of fmt.Sprintf() Really tiny gains here, and doesn't significantly impact readability: BenchmarkSprintf BenchmarkSprintf-10 11528700 91.59 ns/op 32 B/op 1 allocs/op BenchmarkConcat BenchmarkConcat-10 100000000 11.76 ns/op 0 B/op 0 allocs/op Signed-off-by: Sebastiaan van Stijn --- cli/command/config/formatter.go | 2 +- cli/command/container/create.go | 2 +- cli/command/container/run.go | 2 +- cli/command/formatter/container.go | 2 +- cli/command/network/formatter.go | 2 +- cli/command/secret/formatter.go | 2 +- cli/command/system/events.go | 2 +- cli/compose/convert/service.go | 3 +-- 8 files changed, 8 insertions(+), 9 deletions(-) diff --git a/cli/command/config/formatter.go b/cli/command/config/formatter.go index 4aebdb61ac..ee8bc23467 100644 --- a/cli/command/config/formatter.go +++ b/cli/command/config/formatter.go @@ -103,7 +103,7 @@ func (c *configContext) Labels() string { } var joinLabels []string for k, v := range mapLabels { - joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v)) + joinLabels = append(joinLabels, k+"="+v) } return strings.Join(joinLabels, ",") } diff --git a/cli/command/container/create.go b/cli/command/container/create.go index 4758714d1a..0c1574ff86 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -91,7 +91,7 @@ func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, options *createOptio if v == nil { newEnv = append(newEnv, k) } else { - newEnv = append(newEnv, fmt.Sprintf("%s=%s", k, *v)) + newEnv = append(newEnv, k+"="+*v) } } copts.env = *opts.NewListOptsRef(&newEnv, nil) diff --git a/cli/command/container/run.go b/cli/command/container/run.go index c7e680e763..45eca8ffd6 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -101,7 +101,7 @@ func runRun(dockerCli command.Cli, flags *pflag.FlagSet, ropts *runOptions, copt if v == nil { newEnv = append(newEnv, k) } else { - newEnv = append(newEnv, fmt.Sprintf("%s=%s", k, *v)) + newEnv = append(newEnv, k+"="+*v) } } copts.env = *opts.NewListOptsRef(&newEnv, nil) diff --git a/cli/command/formatter/container.go b/cli/command/formatter/container.go index 60ad2fa40b..22b144f98f 100644 --- a/cli/command/formatter/container.go +++ b/cli/command/formatter/container.go @@ -247,7 +247,7 @@ func (c *ContainerContext) Labels() string { var joinLabels []string for k, v := range c.c.Labels { - joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v)) + joinLabels = append(joinLabels, k+"="+v) } return strings.Join(joinLabels, ",") } diff --git a/cli/command/network/formatter.go b/cli/command/network/formatter.go index ed8ac257a0..3229dfdd0a 100644 --- a/cli/command/network/formatter.go +++ b/cli/command/network/formatter.go @@ -103,7 +103,7 @@ func (c *networkContext) Labels() string { var joinLabels []string for k, v := range c.n.Labels { - joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v)) + joinLabels = append(joinLabels, k+"="+v) } return strings.Join(joinLabels, ",") } diff --git a/cli/command/secret/formatter.go b/cli/command/secret/formatter.go index 9102f28856..33c1451249 100644 --- a/cli/command/secret/formatter.go +++ b/cli/command/secret/formatter.go @@ -110,7 +110,7 @@ func (c *secretContext) Labels() string { } var joinLabels []string for k, v := range mapLabels { - joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v)) + joinLabels = append(joinLabels, k+"="+v) } return strings.Join(joinLabels, ",") } diff --git a/cli/command/system/events.go b/cli/command/system/events.go index ddac0fa0a0..08b0e47992 100644 --- a/cli/command/system/events.go +++ b/cli/command/system/events.go @@ -133,7 +133,7 @@ func prettyPrintEvent(out io.Writer, event eventtypes.Message) error { sort.Strings(keys) for _, k := range keys { v := event.Actor.Attributes[k] - attrs = append(attrs, fmt.Sprintf("%s=%s", k, v)) + attrs = append(attrs, k+"="+v) } fmt.Fprintf(out, " (%s)", strings.Join(attrs, ", ")) } diff --git a/cli/compose/convert/service.go b/cli/compose/convert/service.go index 7e0b8cfc99..6935c61968 100644 --- a/cli/compose/convert/service.go +++ b/cli/compose/convert/service.go @@ -1,7 +1,6 @@ package convert import ( - "fmt" "os" "sort" "strings" @@ -605,7 +604,7 @@ func convertEnvironment(source map[string]*string) []string { case nil: output = append(output, name) default: - output = append(output, fmt.Sprintf("%s=%s", name, *value)) + output = append(output, name+"="+*value) } } From d68b361538dfab6933f5eff1448e4299399f3fa3 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 12 Jun 2023 18:20:38 +0200 Subject: [PATCH 2/2] cli/compose/convert: convertEnvironment: sort results All users of this function sorted the results afterwards, so let's do it as part of the function itself. Signed-off-by: Sebastiaan van Stijn --- cli/compose/convert/service.go | 11 ++++------- cli/compose/convert/service_test.go | 2 -- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/cli/compose/convert/service.go b/cli/compose/convert/service.go index 6935c61968..bb9255d581 100644 --- a/cli/compose/convert/service.go +++ b/cli/compose/convert/service.go @@ -133,7 +133,7 @@ func Service( Hosts: convertExtraHosts(service.ExtraHosts), DNSConfig: dnsConfig, Healthcheck: healthcheck, - Env: sortStrings(convertEnvironment(service.Environment)), + Env: convertEnvironment(service.Environment), Labels: AddStackLabel(namespace, service.Labels), Dir: service.WorkingDir, User: service.User, @@ -199,11 +199,6 @@ func getPlacementPreference(preferences []composetypes.PlacementPreferences) []s return result } -func sortStrings(strs []string) []string { - sort.Strings(strs) - return strs -} - func convertServiceNetworks( networks map[string]*composetypes.ServiceNetworkConfig, networkConfigs networkMap, @@ -596,6 +591,8 @@ func convertEndpointSpec(endpointMode string, source []composetypes.ServicePortC } } +// convertEnvironment converts key/value mappings to a slice, and sorts +// the results. func convertEnvironment(source map[string]*string) []string { var output []string @@ -607,7 +604,7 @@ func convertEnvironment(source map[string]*string) []string { output = append(output, name+"="+*value) } } - + sort.Strings(output) return output } diff --git a/cli/compose/convert/service_test.go b/cli/compose/convert/service_test.go index 7a23548e9e..4d48acc3ea 100644 --- a/cli/compose/convert/service_test.go +++ b/cli/compose/convert/service_test.go @@ -3,7 +3,6 @@ package convert import ( "context" "os" - "sort" "strings" "testing" "time" @@ -59,7 +58,6 @@ func TestConvertEnvironment(t *testing.T) { "key": strPtr("value"), } env := convertEnvironment(source) - sort.Strings(env) assert.Check(t, is.DeepEqual([]string{"foo=bar", "key=value"}, env)) }