Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up debug logging, when filtered out #227

Merged
merged 3 commits into from
Oct 21, 2021
Merged

Speed up debug logging, when filtered out #227

merged 3 commits into from
Oct 21, 2021

Conversation

bboreham
Copy link
Collaborator

A benchmark, plus two changes:

  • Filter by log level before anything else - this saves a lot of work walking the stack to find the caller, and a bit more to format the timestamp. As recommended at Filtered-out log.Level lines still cost go-kit/log#14 (comment)
  • Avoid Sprintf when log line is filtered out - defer the Sprintf until after the level check by using an auxilliary struct.

Benchmark results:

name      old time/op    new time/op    delta
Debugf-4    2.46µs ± 6%    0.54µs ± 6%  -78.00%  (p=0.008 n=5+5)

name      old alloc/op   new alloc/op   delta
Debugf-4      848B ± 0%      328B ± 0%  -61.32%  (p=0.008 n=5+5)

name      old allocs/op  new allocs/op  delta
Debugf-4      15.0 ± 0%       9.0 ± 0%  -40.00%  (p=0.008 n=5+5)

Small improvement in behaviour for Infoln(), Warningln(), etc.: they no longer add a newline character to the string. Go-kit log lines are intrinsically a single line, with structured fields so it's wrong to add a newline.
Previously this would come out as msg="My log message\n" in the log.

Extract function to wrap with level, caller, timestamp.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This saves a lot of work walking the stack to find the caller, and a bit
more to format the timestamp.

Recommended at go-kit/log#14 (comment)

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
Defer the Sprintf until after the level check, by using an auxilliary struct

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
Copy link
Contributor

@pracucci pracucci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smart, good job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants