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
```
This commit is contained in:
Clayton Coleman 2019-03-21 20:48:00 -04:00
parent 58fb665646
commit 83c41eab1d
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3

View File

@ -34,7 +34,7 @@ import (
"k8s.io/apiserver/pkg/features"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"github.com/emicklei/go-restful"
restful "github.com/emicklei/go-restful"
"github.com/prometheus/client_golang/prometheus"
)
@ -346,7 +346,10 @@ func cleanUserAgent(ua string) string {
return "Browser"
}
// If an old "kubectl.exe" has passed us its full path, we discard the path portion.
if kubectlExeRegexp.MatchString(ua) {
// avoid an allocation
ua = kubectlExeRegexp.ReplaceAllString(ua, "$1")
}
return ua
}