From d7bc8694beb6a8393a044cb6574a79b6bb944927 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 23 Jun 2023 13:05:24 +0200 Subject: [PATCH 1/5] pkg/cri: replace some fmt.Sprintfs with strconv Teeny-tiny optimizations: BenchmarkSprintf-10 37735996 32.31 ns/op 0 B/op 0 allocs/op BenchmarkItoa-10 591945836 2.031 ns/op 0 B/op 0 allocs/op BenchmarkFormatUint-10 593701444 2.014 ns/op 0 B/op 0 allocs/op Signed-off-by: Sebastiaan van Stijn --- pkg/cri/sbserver/container_create_windows.go | 5 ++--- pkg/cri/sbserver/helpers.go | 18 +++++++++--------- pkg/cri/server/container_create_windows.go | 4 +--- pkg/cri/server/helpers.go | 12 ++++++------ pkg/cri/streaming/remotecommand/exec.go | 3 ++- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/pkg/cri/sbserver/container_create_windows.go b/pkg/cri/sbserver/container_create_windows.go index 7ab18a5aa..9beb2a44a 100644 --- a/pkg/cri/sbserver/container_create_windows.go +++ b/pkg/cri/sbserver/container_create_windows.go @@ -17,7 +17,7 @@ package sbserver import ( - "fmt" + "strconv" imagespec "github.com/opencontainers/image-spec/specs-go/v1" runtime "k8s.io/cri-api/pkg/apis/runtime/v1" @@ -39,9 +39,8 @@ func snapshotterOpts(snapshotterName string, config *runtime.ContainerConfig) ([ case "windows": rootfsSize := config.GetWindows().GetResources().GetRootfsSizeInBytes() if rootfsSize != 0 { - sizeStr := fmt.Sprintf("%d", rootfsSize) labels := map[string]string{ - "containerd.io/snapshot/windows/rootfs.sizebytes": sizeStr, + "containerd.io/snapshot/windows/rootfs.sizebytes": strconv.FormatInt(rootfsSize, 10), } opts = append(opts, snapshots.WithLabels(labels)) } diff --git a/pkg/cri/sbserver/helpers.go b/pkg/cri/sbserver/helpers.go index 5adcbc28d..44b5141cb 100644 --- a/pkg/cri/sbserver/helpers.go +++ b/pkg/cri/sbserver/helpers.go @@ -135,10 +135,10 @@ func (c *criService) getSandboxDevShm(id string) string { // generated is unique as long as sandbox metadata is unique. func makeSandboxName(s *runtime.PodSandboxMetadata) string { return strings.Join([]string{ - s.Name, // 0 - s.Namespace, // 1 - s.Uid, // 2 - fmt.Sprintf("%d", s.Attempt), // 3 + s.Name, // 0 + s.Namespace, // 1 + s.Uid, // 2 + strconv.FormatUint(uint64(s.Attempt), 10), // 3 }, nameDelimiter) } @@ -147,11 +147,11 @@ func makeSandboxName(s *runtime.PodSandboxMetadata) string { // unique. func makeContainerName(c *runtime.ContainerMetadata, s *runtime.PodSandboxMetadata) string { return strings.Join([]string{ - c.Name, // 0: container name - s.Name, // 1: pod name - s.Namespace, // 2: pod namespace - s.Uid, // 3: pod uid - fmt.Sprintf("%d", c.Attempt), // 4: attempt number of creating the container + c.Name, // 0: container name + s.Name, // 1: pod name + s.Namespace, // 2: pod namespace + s.Uid, // 3: pod uid + strconv.FormatUint(uint64(s.Attempt), 10), // 4: attempt number of creating the container }, nameDelimiter) } diff --git a/pkg/cri/server/container_create_windows.go b/pkg/cri/server/container_create_windows.go index 6b3a74c78..ec253bcb1 100644 --- a/pkg/cri/server/container_create_windows.go +++ b/pkg/cri/server/container_create_windows.go @@ -18,7 +18,6 @@ package server import ( "errors" - "fmt" "strconv" imagespec "github.com/opencontainers/image-spec/specs-go/v1" @@ -147,9 +146,8 @@ func snapshotterOpts(snapshotterName string, config *runtime.ContainerConfig) ([ case "windows": rootfsSize := config.GetWindows().GetResources().GetRootfsSizeInBytes() if rootfsSize != 0 { - sizeStr := fmt.Sprintf("%d", rootfsSize) labels := map[string]string{ - "containerd.io/snapshot/windows/rootfs.sizebytes": sizeStr, + "containerd.io/snapshot/windows/rootfs.sizebytes": strconv.FormatInt(rootfsSize, 10), } opts = append(opts, snapshots.WithLabels(labels)) } diff --git a/pkg/cri/server/helpers.go b/pkg/cri/server/helpers.go index 1f5e5cd53..c72e25376 100644 --- a/pkg/cri/server/helpers.go +++ b/pkg/cri/server/helpers.go @@ -99,7 +99,7 @@ func makeSandboxName(s *runtime.PodSandboxMetadata) string { s.Name, // 0 s.Namespace, // 1 s.Uid, // 2 - fmt.Sprintf("%d", s.Attempt), // 3 + strconv.Itoa(int(s.Attempt)), // 3 }, nameDelimiter) } @@ -108,11 +108,11 @@ func makeSandboxName(s *runtime.PodSandboxMetadata) string { // unique. func makeContainerName(c *runtime.ContainerMetadata, s *runtime.PodSandboxMetadata) string { return strings.Join([]string{ - c.Name, // 0: container name - s.Name, // 1: pod name - s.Namespace, // 2: pod namespace - s.Uid, // 3: pod uid - fmt.Sprintf("%d", c.Attempt), // 4: attempt number of creating the container + c.Name, // 0: container name + s.Name, // 1: pod name + s.Namespace, // 2: pod namespace + s.Uid, // 3: pod uid + strconv.FormatUint(uint64(s.Attempt), 10), // 4: attempt number of creating the container }, nameDelimiter) } diff --git a/pkg/cri/streaming/remotecommand/exec.go b/pkg/cri/streaming/remotecommand/exec.go index 5111521ba..1a3febe1b 100644 --- a/pkg/cri/streaming/remotecommand/exec.go +++ b/pkg/cri/streaming/remotecommand/exec.go @@ -36,6 +36,7 @@ import ( "fmt" "io" "net/http" + "strconv" "time" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -76,7 +77,7 @@ func ServeExec(w http.ResponseWriter, req *http.Request, executor Executor, podN Causes: []metav1.StatusCause{ { Type: remotecommandconsts.ExitCodeCauseType, - Message: fmt.Sprintf("%d", rc), + Message: strconv.Itoa(rc), }, }, }, From 710d22366de32fd959d25098a31d0bef9a12319e Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 23 Jun 2023 13:05:41 +0200 Subject: [PATCH 2/5] removes/docker: replace some fmt.Sprintfs with strconv Teeny-tiny optimizations: BenchmarkSprintf-10 37735996 32.31 ns/op 0 B/op 0 allocs/op BenchmarkItoa-10 591945836 2.031 ns/op 0 B/op 0 allocs/op BenchmarkFormatUint-10 593701444 2.014 ns/op 0 B/op 0 allocs/op Signed-off-by: Sebastiaan van Stijn --- remotes/docker/fetcher_fuzz.go | 3 ++- remotes/docker/fetcher_test.go | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/remotes/docker/fetcher_fuzz.go b/remotes/docker/fetcher_fuzz.go index b98886c59..849670a18 100644 --- a/remotes/docker/fetcher_fuzz.go +++ b/remotes/docker/fetcher_fuzz.go @@ -25,6 +25,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "strconv" refDocker "github.com/containerd/containerd/reference/docker" ) @@ -37,7 +38,7 @@ func FuzzFetcher(data []byte) int { s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { rw.Header().Set("content-range", fmt.Sprintf("bytes %d-%d/%d", 0, dataLen-1, dataLen)) - rw.Header().Set("content-length", fmt.Sprintf("%d", dataLen)) + rw.Header().Set("content-length", strconv.Itoa(dataLen)) rw.Write(data) })) defer s.Close() diff --git a/remotes/docker/fetcher_test.go b/remotes/docker/fetcher_test.go index 07cc90a24..695d6045f 100644 --- a/remotes/docker/fetcher_test.go +++ b/remotes/docker/fetcher_test.go @@ -25,6 +25,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "strconv" "testing" "github.com/stretchr/testify/assert" @@ -39,8 +40,8 @@ func TestFetcherOpen(t *testing.T) { if start > 0 { rw.Header().Set("content-range", fmt.Sprintf("bytes %d-127/128", start)) } - rw.Header().Set("content-length", fmt.Sprintf("%d", len(content[start:]))) - rw.Write(content[start:]) + rw.Header().Set("content-length", strconv.Itoa(len(content[start:]))) + _, _ = rw.Write(content[start:]) })) defer s.Close() From 06765c9ef5bece82ca33cd167a9f99cd78533fa8 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 23 Jun 2023 13:06:03 +0200 Subject: [PATCH 3/5] oci: replace some fmt.Sprintfs with strconv Teeny-tiny optimizations: BenchmarkSprintf-10 37735996 32.31 ns/op 0 B/op 0 allocs/op BenchmarkItoa-10 591945836 2.031 ns/op 0 B/op 0 allocs/op BenchmarkFormatUint-10 593701444 2.014 ns/op 0 B/op 0 allocs/op Signed-off-by: Sebastiaan van Stijn --- oci/spec_opts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oci/spec_opts.go b/oci/spec_opts.go index 359a1af23..55ccaf09a 100644 --- a/oci/spec_opts.go +++ b/oci/spec_opts.go @@ -420,7 +420,7 @@ func WithImageConfigArgs(image Image, args []string) SpecOpts { if err := WithUser(config.User)(ctx, client, c, s); err != nil { return err } - return WithAdditionalGIDs(fmt.Sprintf("%d", s.Process.User.UID))(ctx, client, c, s) + return WithAdditionalGIDs(strconv.FormatInt(int64(s.Process.User.UID), 10))(ctx, client, c, s) } // we should query the image's /etc/group for additional GIDs // even if there is no specified user in the image config From 11a5dd826577d6caea01b7ec1a9a16bc6db202d1 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 23 Jun 2023 13:06:18 +0200 Subject: [PATCH 4/5] snapshots: replace some fmt.Sprintfs with strconv Teeny-tiny optimizations: BenchmarkSprintf-10 37735996 32.31 ns/op 0 B/op 0 allocs/op BenchmarkItoa-10 591945836 2.031 ns/op 0 B/op 0 allocs/op BenchmarkFormatUint-10 593701444 2.014 ns/op 0 B/op 0 allocs/op Signed-off-by: Sebastiaan van Stijn --- snapshots/storage/bolt.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/snapshots/storage/bolt.go b/snapshots/storage/bolt.go index 894ac2e02..095a741bd 100644 --- a/snapshots/storage/bolt.go +++ b/snapshots/storage/bolt.go @@ -21,6 +21,7 @@ import ( "encoding/binary" "errors" "fmt" + "strconv" "strings" "time" @@ -89,7 +90,7 @@ func GetInfo(ctx context.Context, key string) (string, snapshots.Info, snapshots return "", snapshots.Info{}, snapshots.Usage{}, err } - return fmt.Sprintf("%d", id), si, su, nil + return strconv.FormatUint(id, 10), si, su, nil } // UpdateInfo updates an existing snapshot info's data @@ -184,7 +185,7 @@ func GetSnapshot(ctx context.Context, key string) (s Snapshot, err error) { return fmt.Errorf("snapshot does not exist: %w", errdefs.ErrNotFound) } - s.ID = fmt.Sprintf("%d", readID(sbkt)) + s.ID = strconv.FormatUint(readID(sbkt), 10) s.Kind = readKind(sbkt) if s.Kind != snapshots.KindActive && s.Kind != snapshots.KindView { @@ -279,7 +280,7 @@ func CreateSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string } } - s.ID = fmt.Sprintf("%d", id) + s.ID = strconv.FormatUint(id, 10) s.Kind = kind return nil }) @@ -336,7 +337,7 @@ func Remove(ctx context.Context, key string) (string, snapshots.Kind, error) { return "", 0, err } - return fmt.Sprintf("%d", id), si.Kind, nil + return strconv.FormatUint(id, 10), si.Kind, nil } // CommitActive renames the active snapshot transaction referenced by `key` @@ -411,7 +412,7 @@ func CommitActive(ctx context.Context, key, name string, usage snapshots.Usage, return "", err } - return fmt.Sprintf("%d", id), nil + return strconv.FormatUint(id, 10), nil } // IDMap returns all the IDs mapped to their key @@ -424,7 +425,7 @@ func IDMap(ctx context.Context) (map[string]string, error) { return nil } id := readID(bkt.Bucket(k)) - m[fmt.Sprintf("%d", id)] = string(k) + m[strconv.FormatUint(id, 10)] = string(k) return nil }) }); err != nil { @@ -490,7 +491,7 @@ func createBucketIfNotExists(ctx context.Context, fn func(context.Context, *bolt func parents(bkt, pbkt *bolt.Bucket, parent uint64) (parents []string, err error) { for { - parents = append(parents, fmt.Sprintf("%d", parent)) + parents = append(parents, strconv.FormatUint(parent, 10)) parentKey := pbkt.Get(bucketKeyParent) if len(parentKey) == 0 { From b76cd4d9fd8846ed9b04808c4782eb20f83711db Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 23 Jun 2023 13:06:48 +0200 Subject: [PATCH 5/5] replace some fmt.Sprintfs with strconv Teeny-tiny optimizations: BenchmarkSprintf-10 37735996 32.31 ns/op 0 B/op 0 allocs/op BenchmarkItoa-10 591945836 2.031 ns/op 0 B/op 0 allocs/op BenchmarkFormatUint-10 593701444 2.014 ns/op 0 B/op 0 allocs/op Signed-off-by: Sebastiaan van Stijn --- cmd/ctr/commands/snapshots/snapshots.go | 3 ++- integration/client/daemon_config_linux_test.go | 4 ++-- integration/nri_test.go | 2 +- pkg/cri/sbserver/helpers.go | 2 +- pkg/cri/server/helpers.go | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/cmd/ctr/commands/snapshots/snapshots.go b/cmd/ctr/commands/snapshots/snapshots.go index 4f74fe774..c8ace2d5b 100644 --- a/cmd/ctr/commands/snapshots/snapshots.go +++ b/cmd/ctr/commands/snapshots/snapshots.go @@ -23,6 +23,7 @@ import ( "io" "os" "path/filepath" + "strconv" "strings" "text/tabwriter" "time" @@ -207,7 +208,7 @@ var usageCommand = cli.Command{ var displaySize func(int64) string if context.Bool("b") { displaySize = func(s int64) string { - return fmt.Sprintf("%d", s) + return strconv.FormatInt(s, 10) } } else { displaySize = func(s int64) string { diff --git a/integration/client/daemon_config_linux_test.go b/integration/client/daemon_config_linux_test.go index 86f9dc3f9..f45d619b8 100644 --- a/integration/client/daemon_config_linux_test.go +++ b/integration/client/daemon_config_linux_test.go @@ -18,9 +18,9 @@ package client import ( "bufio" - "fmt" "os" "path/filepath" + "strconv" "strings" "syscall" "testing" @@ -133,7 +133,7 @@ func TestDaemonCustomCgroup(t *testing.T) { t.Skip("skip TestDaemonCustomCgroup since no cgroup path available") } - customCgroup := fmt.Sprintf("%d", time.Now().Nanosecond()) + customCgroup := strconv.Itoa(time.Now().Nanosecond()) configTOML := ` version = 2 [cgroup] diff --git a/integration/nri_test.go b/integration/nri_test.go index a2fe2e7dc..22ceccf8d 100644 --- a/integration/nri_test.go +++ b/integration/nri_test.go @@ -602,7 +602,7 @@ func (tc *nriTest) setup() { tc.prefix = strings.ToLower(tc.name) } if tc.namespace == "" { - tc.namespace = tc.prefix + "-" + fmt.Sprintf("%d", os.Getpid()) + tc.namespace = tc.prefix + "-" + strconv.Itoa(os.Getpid()) } tc.sbCfg = make(map[string]*runtime.PodSandboxConfig) diff --git a/pkg/cri/sbserver/helpers.go b/pkg/cri/sbserver/helpers.go index 44b5141cb..68abc1e4d 100644 --- a/pkg/cri/sbserver/helpers.go +++ b/pkg/cri/sbserver/helpers.go @@ -151,7 +151,7 @@ func makeContainerName(c *runtime.ContainerMetadata, s *runtime.PodSandboxMetada s.Name, // 1: pod name s.Namespace, // 2: pod namespace s.Uid, // 3: pod uid - strconv.FormatUint(uint64(s.Attempt), 10), // 4: attempt number of creating the container + strconv.FormatUint(uint64(c.Attempt), 10), // 4: attempt number of creating the container }, nameDelimiter) } diff --git a/pkg/cri/server/helpers.go b/pkg/cri/server/helpers.go index c72e25376..447e97088 100644 --- a/pkg/cri/server/helpers.go +++ b/pkg/cri/server/helpers.go @@ -112,7 +112,7 @@ func makeContainerName(c *runtime.ContainerMetadata, s *runtime.PodSandboxMetada s.Name, // 1: pod name s.Namespace, // 2: pod namespace s.Uid, // 3: pod uid - strconv.FormatUint(uint64(s.Attempt), 10), // 4: attempt number of creating the container + strconv.FormatUint(uint64(c.Attempt), 10), // 4: attempt number of creating the container }, nameDelimiter) }