Repository navigation
Description
At the moment, https://pkg.go.dev/golang.org/x/exp/slog passes Logger around by pointer. The implication is that functions like Logger.With need one additional allocation, for the new Logger instance.
The alternative is to pass it as value, like go-logr does. This improves performance for With (see https://github.com/golang/exp/compare/master...pohly:exp:logger-as-value?expand=1) and others (not tested yet):
name old time/op new time/op delta
With/Text_discard/no_parameters-36 165ns ±18% 135ns ±25% ~ (p=0.222 n=5+5)
With/Text_discard/1_key/value_pair-36 397ns ± 2% 356ns ± 1% -10.39% (p=0.008 n=5+5)
With/Text_discard/5_key/value_pairs-36 1.64µs ± 1% 1.54µs ± 1% -6.26% (p=0.008 n=5+5)
With/JSON_discard/no_parameters-36 210ns ± 4% 175ns ± 5% -16.38% (p=0.008 n=5+5)
With/JSON_discard/1_key/value_pair-36 435ns ± 1% 404ns ± 3% -7.02% (p=0.008 n=5+5)
With/JSON_discard/5_key/value_pairs-36 2.03µs ± 1% 1.97µs ± 1% -3.06% (p=0.008 n=5+5)
name old alloc/op new alloc/op delta
With/Text_discard/no_parameters-36 169B ± 0% 153B ± 0% -9.47% (p=0.008 n=5+5)
With/Text_discard/1_key/value_pair-36 290B ± 0% 274B ± 0% ~ (p=0.079 n=4+5)
With/Text_discard/5_key/value_pairs-36 1.00kB ± 0% 0.98kB ± 0% -1.70% (p=0.008 n=5+5)
With/JSON_discard/no_parameters-36 169B ± 0% 153B ± 0% -9.47% (p=0.008 n=5+5)
With/JSON_discard/1_key/value_pair-36 307B ± 0% 290B ± 0% -5.54% (p=0.008 n=5+5)
With/JSON_discard/5_key/value_pairs-36 1.26kB ± 0% 1.24kB ± 0% -1.35% (p=0.008 n=5+5)
name old allocs/op new allocs/op delta
With/Text_discard/no_parameters-36 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.008 n=5+5)
With/Text_discard/1_key/value_pair-36 6.00 ± 0% 5.00 ± 0% -16.67% (p=0.008 n=5+5)
With/Text_discard/5_key/value_pairs-36 12.0 ± 0% 11.0 ± 0% -8.33% (p=0.008 n=5+5)
With/JSON_discard/no_parameters-36 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.008 n=5+5)
With/JSON_discard/1_key/value_pair-36 7.00 ± 0% 6.00 ± 0% -14.29% (p=0.008 n=5+5)
With/JSON_discard/5_key/value_pairs-36 12.0 ± 0% 11.0 ± 0% -8.33% (p=0.008 n=5+5)
The downside is that the size of Logger should not grow indefinitely in the future. But that is true anyway (because these calls must clone it) and also seems unlikely.
Related to this is the question whether calling any of the methods for the null Logger should be valid. Currently they panic because Logger.handler will be nil. I think this is the right choice given that they are performance-sensitive, but I wanted to ask anyway...
Activity
pohly commentedon Mar 19, 2023
cc @jba
jba commentedon Mar 21, 2023
Hmm, I thought I benchmarked this and the allocation was eliminated. I'll try to replicate your benchmarks.
pohly commentedon Mar 21, 2023
Regarding zero
Logger: in go-logr, methods panicked initially like they do now in slog. We got convinced in go-logr/logr#153 to handle this by discarding log output.jba commentedon Mar 21, 2023
Oh, I see, the "With" benchmarks are your addition.
I didn't care about
Logger.Withbecause I don't see it as being on the critical path. I believe people will callWithto set up a Logger, which you'll then use a lot. Am I wrong about that? (I know @ChrisHines thinks I am.) What is your experience?Loggerwas a value type for a while because I thought a pointer type would allocate even for output methods (Logger.Info and so on). I was wrong about that, because of inlining. Using a pointer is more idiomatic; on the other hand, Loggers are immutable so a value type makes sense too.pohly commentedon Mar 21, 2023
That depends on how users do contextual logging.
One school of thought says that a logger should extract additional values from the context. Then the critical path only does
context.WithValue.The other school of thought (me including) points out that this only works in monolithic code bases where the logger configuration knows about all context keys. There also cannot be many of those, otherwise each log invocation would have to do many different
context.Context.Valuecalls, of which most don't return anything. Then it makes more sense to create a new logger throughWithand pass that down into the call chain.IMHO it would be good if slog supported both approaches equally well. Note that a future
WithCallDepth(#59145) would have the same performance impact when using a pointer. In the case ofWithCallDepththere's no question that this is going to be called often (= each time such a helper function gets called).ChrisHines commentedon Mar 21, 2023
@jba Code I've been reviewing the last few days using a different structured logging package has a flow like:
jba commentedon Mar 22, 2023
@ChrisHines, that code could easily be rewritten with an
Attrvariable, or in general with a[]Attr. A more fundamental use ofLogger.Within my mind is when some component takes aLogger, and you want to set some attributes for all log output from that component:ChrisHines commentedon Mar 22, 2023
@jba Yes, it could be written as you suggest, but in my experience it it tends to be less convenient and noisier code. Maybe you have some idea that I haven't thought of, could you show how you would write it?
pohly commentedon Mar 22, 2023
I agree.
time.Timeis another example in the stdlib.So do we have consensus that passing by value is the way to go? API design seems to allow both, and by value is more efficient.
ianthehat commentedon Mar 23, 2023
@ChrisHines how about
which should be much faster and only 1 extra line.
I do think that Logger should pass by value, but I think that Logger.With is a much larger hammer that should only be used for cases where you are going to re-use the logger, not for single message construction.
ChrisHines commentedon Mar 23, 2023
Of course that works @ianthehat , but the
slogAPI does not guide a developer to write code that way and it is not as nice on the eyes as usinglogger.With, so I think it is ergonomically poor. I think it is very unfortunate to publish an API that results in the most convenient way to use it being the wrong thing to do in common scenarios. Theslog.Group("", attrs...)trick is clever, but I don't want to have to repeatedly explain that in code review as either the author or the reviewer.I think we should strive for much better, especially in the standard library.
9 remaining items
rsc commentedon Apr 12, 2023
time.Time was mentioned above. Speaking as someone who has had to extend time.Time over time and been quite constrained by needing to keep it small to make being a value reasonable, changing Logger to be a struct will limit us considerably in the future. It does not seem worth painting ourselves into that corner.
ianthehat commentedon Apr 12, 2023
I don't think it would paint you into a corner, if you really needed additional fields, you just make Logger point to an intermediate that held the handler and the extra fields, which would essentially put you right back where we are now in terms of allocations and cost.
jba commentedon Apr 12, 2023
That would result in shared state on a copy, which might be wrong.
pohly commentedon Apr 13, 2023
My two cents: as seen in the proposal to add
WithCallDepth,Loggeris meant to be just a thin convenience layer aroundHandler. It seems very unlikely that this will change, whereas the use case from #59126 (comment) with constructing a newLoggerfrom an existingHandlerseems more likely. So which use case should the implementation prefer?Having said that, I'm fine with whatever gets decided.
rsc commentedon Apr 19, 2023
Based on the discussion above, this proposal seems like a likely decline.
— rsc for the proposal review group
rsc commentedon May 3, 2023
No change in consensus, so declined.
— rsc for the proposal review group