Skip to content

Commit 59b4f47

Browse files
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 ```
1 parent 83c41ea commit 59b4f47

File tree

1 file changed

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

1 file changed

+10
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bufio"
2121
"net"
2222
"net/http"
23+
"net/url"
2324
"regexp"
2425
"strconv"
2526
"strings"
@@ -234,7 +235,7 @@ func RecordLongRunning(req *http.Request, requestInfo *request.RequestInfo, comp
234235
// a request. verb must be uppercase to be backwards compatible with existing monitoring tooling.
235236
func MonitorRequest(req *http.Request, verb, group, version, resource, subresource, scope, component, contentType string, httpCode, respSize int, elapsed time.Duration) {
236237
reportedVerb := cleanVerb(verb, req)
237-
dryRun := cleanDryRun(req.URL.Query()["dryRun"])
238+
dryRun := cleanDryRun(req.URL)
238239
client := cleanUserAgent(utilnet.GetHTTPClient(req))
239240
elapsedMicroseconds := float64(elapsed / time.Microsecond)
240241
elapsedSeconds := elapsed.Seconds()
@@ -331,12 +332,19 @@ func cleanVerb(verb string, request *http.Request) string {
331332
return reportedVerb
332333
}
333334

334-
func cleanDryRun(dryRun []string) string {
335+
func cleanDryRun(u *url.URL) string {
336+
// avoid allocating when we don't see dryRun in the query
337+
if !strings.Contains(u.RawQuery, "dryRun") {
338+
return ""
339+
}
340+
dryRun := u.Query()["dryRun"]
335341
if errs := validation.ValidateDryRun(nil, dryRun); len(errs) > 0 {
336342
return "invalid"
337343
}
338344
// Since dryRun could be valid with any arbitrarily long length
339345
// we have to dedup and sort the elements before joining them together
346+
// TODO: this is a fairly large allocation for what it does, consider
347+
// a sort and dedup in a single pass
340348
return strings.Join(utilsets.NewString(dryRun...).List(), ",")
341349
}
342350

0 commit comments

Comments
 (0)