From b8213460055c2cc7dd3be84b848a24c9b2b625e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez=20=28inkel=29?= Date: Fri, 6 Jun 2025 08:27:49 -0300 Subject: [PATCH] Concatenate string instead of using fmt.Sprintf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The call to `fmt.Sprintf` does virtually the same as concatenating the strings, but incurs in minor overhead from having to call a function, and performs an additional allocation. I've made a small benchmark for this and when run against current `master` and this branch, the results are significant for this function: goos: darwin goarch: arm64 pkg: k8s.io/client-go/transport cpu: Apple M1 Pro │ bart.base.log │ bart.concat.log │ │ sec/op │ sec/op vs base │ BearerAuthRoundTripper-10 361.3n ± 31% 295.5n ± 11% -18.21% (p=0.000 n=20) │ bart.base.log │ bart.concat.log │ │ B/op │ B/op vs base │ BearerAuthRoundTripper-10 768.0 ± 0% 752.0 ± 0% -2.08% (p=0.000 n=20) │ bart.base.log │ bart.concat.log │ │ allocs/op │ allocs/op vs base │ BearerAuthRoundTripper-10 6.000 ± 0% 5.000 ± 0% -16.67% (p=0.000 n=20) Considering this method is likely used in many installations, the gains, while small, adds up to bigger savings Kubernetes-commit: 92a0e422df2d0d36c25f0b5c829c571b93250600 --- transport/round_trippers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transport/round_trippers.go b/transport/round_trippers.go index 39fcebd94..a50977671 100644 --- a/transport/round_trippers.go +++ b/transport/round_trippers.go @@ -319,7 +319,7 @@ func (rt *bearerAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, token = refreshedToken.AccessToken } } - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) + req.Header.Set("Authorization", "Bearer "+token) return rt.rt.RoundTrip(req) }