Repository navigation
Improve perf with level guard in slogWrapper calls - #1374
Merged
nodece merged 1 commit intoMay 29, 2025
Merged
Conversation
Previously, calls to methods such as (*slogWrapper).Debug and (*slogWrapper).Debugf always resulted in calls to fmt.Sprint and fmt.Sprintf respectively, regardless of whether the log level in question was even enabled. Calls to fmt.Sprintf in particular can be costly from a performance standpoint. This change prevents such calls unless the log level in question is currently enabled. Benchmarks on an Apple M3 Max result in a ~7x speed improvement in 'f' method variants when the log level in question is disabled.
lhotari
approved these changes
May 28, 2025
lhotari
left a comment
Member
There was a problem hiding this comment.
LGTM, great contribution @gmurphy-cogito. Thank you!
nodece
approved these changes
May 29, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Previously, calls to methods such as
(*slogWrapper).Debugand(*slogWrapper).Debugfalways resulted in calls tofmt.Sprintandfmt.Sprintfrespectively, regardless of whether the log level in question was even enabled. Calls tofmt.Sprintfin particular can be costly from a performance standpoint.We saw this resulting in significant performance impact in production instances, due to calls to
(*slogWrapper).Debugfeven though debug logging was disabled.Modifications
This change prevents such calls unless the log level in question is currently enabled. Benchmarks on an Apple M3 Max result in a ~7x speed improvement in 'f' method variants when the log level in question is disabled:
Verifying this change
This change is already covered by existing tests, such as those in
pulsar/log/wrapper_slog_test.gowhich confirm logs are output when the corresponding log level is enabled.Does this pull request potentially affect one of the following parts:
If
yeswas chosen, please highlight the changesDocumentation