Skip to content

Commit

Permalink
funcr: optimize WithValues/WithName/WithContext
Browse files Browse the repository at this point in the history
All of these functions indirectly copied the entire Options struct because it
gets stored by value in the Formatter and thus fnlogger. The struct is
read-only, therefore sharing a single copy by pointer via different logger
instances is possible. Performance for the context value benchmark gets better:

name                             old time/op    new time/op    delta
NewContext1Percent-36              32.4µs ± 5%    31.8µs ± 3%    ~     (p=0.421 n=5+5)
NewContext100PercentDisabled-36    51.0µs ± 4%    49.4µs ± 2%    ~     (p=0.095 n=5+5)
NewContext100Percent-36             572µs ± 4%     534µs ± 2%  -6.77%  (p=0.008 n=5+5)

name                             old alloc/op   new alloc/op   delta
NewContext1Percent-36              11.3kB ± 0%    11.2kB ± 0%  -0.71%  (p=0.008 n=5+5)
NewContext100PercentDisabled-36    19.2kB ± 0%    19.2kB ± 0%    ~     (all equal)
NewContext100Percent-36             178kB ± 0%     170kB ± 0%  -4.48%  (p=0.008 n=5+5)

name                             old allocs/op  new allocs/op  delta
NewContext1Percent-36                 220 ± 0%       220 ± 0%    ~     (all equal)
NewContext100PercentDisabled-36       300 ± 0%       300 ± 0%    ~     (all equal)
NewContext100Percent-36             2.20k ± 0%     2.20k ± 0%    ~     (all equal)
  • Loading branch information
pohly committed Nov 18, 2022
1 parent 7ff155d commit b6c08cc
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions funcr/funcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func newFormatter(opts Options, outfmt outputFormat) Formatter {
prefix: "",
values: nil,
depth: 0,
opts: opts,
opts: &opts,
}
return f
}
Expand All @@ -259,7 +259,7 @@ type Formatter struct {
values []interface{}
valuesStr string
depth int
opts Options
opts *Options
}

// outputFormat indicates which outputFormat to use.
Expand Down

0 comments on commit b6c08cc

Please sign in to comment.