Skip to content

Commit 83c41ea

Browse files
Avoid an allocation on all requests when checking for an old user agent
ReplaceAllStrings always allocates, while scanning a relatively short regex twice is slightly more CPU immediately but less later. ``` BenchmarkGet-12 100000 108824 ns/op 17818 B/op 152 allocs/op BenchmarkGet-12 100000 108013 ns/op 17732 B/op 149 allocs/op ```
1 parent 58fb665 commit 83c41ea

File tree

1 file changed

+5
-2
lines changed
  • staging/src/k8s.io/apiserver/pkg/endpoints/metrics

1 file changed

+5
-2
lines changed

staging/src/k8s.io/apiserver/pkg/endpoints/metrics/metrics.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
"k8s.io/apiserver/pkg/features"
3535
utilfeature "k8s.io/apiserver/pkg/util/feature"
3636

37-
"github.com/emicklei/go-restful"
37+
restful "github.com/emicklei/go-restful"
3838
"github.com/prometheus/client_golang/prometheus"
3939
)
4040

@@ -346,7 +346,10 @@ func cleanUserAgent(ua string) string {
346346
return "Browser"
347347
}
348348
// If an old "kubectl.exe" has passed us its full path, we discard the path portion.
349-
ua = kubectlExeRegexp.ReplaceAllString(ua, "$1")
349+
if kubectlExeRegexp.MatchString(ua) {
350+
// avoid an allocation
351+
ua = kubectlExeRegexp.ReplaceAllString(ua, "$1")
352+
}
350353
return ua
351354
}
352355

0 commit comments

Comments
 (0)