log: avoid stack lookups when not needed/used#28069
Conversation
| }) | ||
| } | ||
| if locationEnabled.Load() { | ||
| record.Call = stack.Caller(skip) |
There was a problem hiding this comment.
It's problematic. e.g. in the log/handler_glog.go, L188, we assume this is available.
But I agree it's a good optimization, but just need more checks.
There was a problem hiding this comment.
Hm, interesting. I dug into this, it turns out that r.Call.String() == %!v(NOFUNC).
due to
// String implements fmt.Stinger. It is equivalent to fmt.Sprintf("%v", c).
func (c Call) String() string {
return fmt.Sprint(c)
}So there's no crash, just it won't print the backtrace.
There was a problem hiding this comment.
Should be noted, though: the Call is stack.Call -- not a pointer. Nothing is nil here. And it goes on
type Call struct {
frame runtime.Frame
}(no pointers)
type Frame struct {
// PC is the program counter for the location in this frame.
// For a frame that calls another frame, this will be the
// program counter of a call instruction. Because of inlining,
// multiple frames may have the same PC value, but different
// symbolic information.
PC uintptr
...This struct too is (nearly) pointer-free.
There was a problem hiding this comment.
Ha I spent two hours yesterday trying to create a test to hit this, but couldn't because no pointer. So it looks good to me
|
Sweet. On my machine it's 1000ns vs 98 with and without the call. |
| // storeStackRecords is an atomic flag controlling whether the log handler needs | ||
| // to store the callsite stack. This is needed in case any handler wants to | ||
| // print locations (locationEnabled), use vmodule, or print full stacks (BacktraceAt). | ||
| var stackEnabled atomic.Bool |
There was a problem hiding this comment.
linter's gonna lint :P field and comment name don't match
Avoids the somewhat expensive stack.Caller invocation by checking if it is needed
Avoids the somewhat expensive stack.Caller invocation by checking if it is needed
This reverts commit f32ea1a.
This reverts commit f32ea1a.
We can avoid the somewhat expensive
stack.Callerinvocation by checking the atomic global.Closes #28064