Skip to content

Commit

Permalink
structured output: reorder type switch
Browse files Browse the repository at this point in the history
Checking common types first is slightly faster.

No difference for simple benchmarks:

$ $GOPATH/bin/benchstat /tmp/strconv /tmp/reorder
name                               old time/op    new time/op    delta
Logging/container/structured-36      39.5µs ± 2%    39.6µs ± 1%   ~     (p=1.000 n=5+5)
Logging/error-value/structured-36    3.13µs ± 0%    3.14µs ± 4%   ~     (p=0.730 n=4+5)
Logging/error/structured-36          3.09µs ± 2%    3.13µs ± 3%   ~     (p=0.548 n=5+5)
Logging/simple/structured-36         2.86µs ± 4%    2.91µs ± 2%   ~     (p=0.310 n=5+5)
Logging/values/structured-36         4.77µs ± 4%    4.77µs ± 5%   ~     (p=0.841 n=5+5)

name                               old alloc/op   new alloc/op   delta
Logging/container/structured-36      9.35kB ± 0%    9.35kB ± 0%   ~     (p=0.095 n=5+4)
Logging/error-value/structured-36      312B ± 0%      312B ± 0%   ~     (all equal)
Logging/error/structured-36            312B ± 0%      312B ± 0%   ~     (all equal)
Logging/simple/structured-36           288B ± 0%      288B ± 0%   ~     (all equal)
Logging/values/structured-36           464B ± 0%      464B ± 0%   ~     (all equal)

name                               old allocs/op  new allocs/op  delta
Logging/container/structured-36        62.0 ± 0%      62.0 ± 0%   ~     (all equal)
Logging/error-value/structured-36      6.00 ± 0%      6.00 ± 0%   ~     (all equal)
Logging/error/structured-36            6.00 ± 0%      6.00 ± 0%   ~     (all equal)
Logging/simple/structured-36           5.00 ± 0%      5.00 ± 0%   ~     (all equal)
Logging/values/structured-36           11.0 ± 0%      11.0 ± 0%   ~     (all equal)

It's a bigger win for a real kubelet log:

$ $GOPATH/bin/benchstat /tmp/strconv-kubelet /tmp/reorder-kubelet
name                              old time/op    new time/op    delta
Logging/v5/kubelet/structured-36     1.17s ± 2%     1.15s ± 1%  -1.37%  (p=0.016 n=5+5)

name                              old alloc/op   new alloc/op   delta
Logging/v5/kubelet/structured-36     525MB ± 0%     525MB ± 0%    ~     (p=0.548 n=5+5)

name                              old allocs/op  new allocs/op  delta
Logging/v5/kubelet/structured-36     3.86M ± 0%     3.86M ± 0%    ~     (p=0.079 n=5+5)

        Args:
         total: 310746
         strings: 106904 (34%)
           with line breaks: 11 (0% of all arguments)
           with API objects: 529 (0% of all arguments)
             types and their number of usage: Container:529
         numbers: 14563 (4%)
         ObjectRef: 169306 (54%)
         others: 19973 (6%)
  • Loading branch information
pohly committed Nov 26, 2021
1 parent aac832f commit 6749fe1
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,14 @@ func kvListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
}
b.WriteByte('=')

// The type checks are sorted so that more frequently used ones
// come first because that is then faster in the common
// cases. In Kubernetes, ObjectRef (a Stringer) is more common
// than plain strings
// (https://github.com/kubernetes/kubernetes/pull/106594#issuecomment-975526235).
switch v := v.(type) {
case fmt.Stringer:
b.WriteString(strconv.Quote(v.String()))
case string:
b.WriteString(strconv.Quote(v))
case error:
Expand All @@ -845,8 +852,6 @@ func kvListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
// because it does not escape unicode characters, which is
// expected by one test!?
b.WriteString(fmt.Sprintf("%+q", v))
case fmt.Stringer:
b.WriteString(strconv.Quote(v.String()))
default:
b.WriteString(fmt.Sprintf("%+v", v))
}
Expand Down

0 comments on commit 6749fe1

Please sign in to comment.