Avoid 3 object creations when no dryRun parameter is provided

These allocations should only occur rarely.

```
BenchmarkGet-12          	  100000	    108013 ns/op	   17732 B/op	     149 allocs/op

BenchmarkGet-12          	  100000	    109045 ns/op	   17608 B/op	     146 allocs/op
```
This commit is contained in:
Clayton Coleman 2019-03-21 20:57:36 -04:00
parent 83c41eab1d
commit 59b4f47b22
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3

View File

@ -20,6 +20,7 @@ import (
"bufio"
"net"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
@ -234,7 +235,7 @@ func RecordLongRunning(req *http.Request, requestInfo *request.RequestInfo, comp
// a request. verb must be uppercase to be backwards compatible with existing monitoring tooling.
func MonitorRequest(req *http.Request, verb, group, version, resource, subresource, scope, component, contentType string, httpCode, respSize int, elapsed time.Duration) {
reportedVerb := cleanVerb(verb, req)
dryRun := cleanDryRun(req.URL.Query()["dryRun"])
dryRun := cleanDryRun(req.URL)
client := cleanUserAgent(utilnet.GetHTTPClient(req))
elapsedMicroseconds := float64(elapsed / time.Microsecond)
elapsedSeconds := elapsed.Seconds()
@ -331,12 +332,19 @@ func cleanVerb(verb string, request *http.Request) string {
return reportedVerb
}
func cleanDryRun(dryRun []string) string {
func cleanDryRun(u *url.URL) string {
// avoid allocating when we don't see dryRun in the query
if !strings.Contains(u.RawQuery, "dryRun") {
return ""
}
dryRun := u.Query()["dryRun"]
if errs := validation.ValidateDryRun(nil, dryRun); len(errs) > 0 {
return "invalid"
}
// Since dryRun could be valid with any arbitrarily long length
// we have to dedup and sort the elements before joining them together
// TODO: this is a fairly large allocation for what it does, consider
// a sort and dedup in a single pass
return strings.Join(utilsets.NewString(dryRun...).List(), ",")
}