mirror of
https://github.com/kubernetes/client-go.git
synced 2026-02-21 07:32:21 +00:00
Concatenate string instead of using fmt.Sprintf
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
This commit is contained in:
committed by
Kubernetes Publisher
parent
65de5216f1
commit
b821346005
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user