Skip to content

Improve perf with level guard in slogWrapper calls - #1374

Merged
nodece merged 1 commit into
apache:masterfrom
gmurphy-cogito:slogwrapper-perf-improvement
May 29, 2025
Merged

nodece merged 1 commit into
apache:masterfrom
gmurphy-cogito:slogwrapper-perf-improvement

Conversation

@gmurphy-cogito

@gmurphy-cogito gmurphy-cogito commented May 21, 2025 •

Copy link
Copy Markdown
Contributor

Motivation

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.

We saw this resulting in significant performance impact in production instances, due to calls to (*slogWrapper).Debugf even 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:

BenchmarkDebugfNoLvlCheckWritten-14      	 1755528	       676.3 ns/op	     232 B/op	       5 allocs/op
BenchmarkDebugfLvlCheckWritten-14        	 1789832	       668.6 ns/op	     232 B/op	       5 allocs/op
BenchmarkDebugfNoLvlCheckUnwritten-14    	 3885044	       307.9 ns/op	     232 B/op	       5 allocs/op
BenchmarkDebugfLvlCheckUnwritten-14      	29590905	        41.72 ns/op	      24 B/op	       1 allocs/op
BenchmarkDebugNoLvlCheckWritten-14       	 1808078	       667.2 ns/op	     232 B/op	       5 allocs/op
BenchmarkDebugLvlCheckWritten-14         	 1777320	       670.4 ns/op	     232 B/op	       5 allocs/op
BenchmarkDebugNoLvlCheckUnwritten-14     	29176137	        41.18 ns/op	      24 B/op	       1 allocs/op
BenchmarkDebugLvlCheckUnwritten-14       	29249030	        41.43 ns/op	      24 B/op	       1 allocs/op

Verifying this change

  • Make sure that the change passes the CI checks.

This change is already covered by existing tests, such as those in pulsar/log/wrapper_slog_test.go which confirm logs are output when the corresponding log level is enabled.

Does this pull request potentially affect one of the following parts:

If yes was chosen, please highlight the changes

  • Dependencies (does it add or upgrade a dependency): no
  • The public API: no
  • The schema: no
  • The default values of configurations: no
  • The wire protocol: no

Documentation

  • Does this pull request introduce a new feature? no

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 lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, great contribution @gmurphy-cogito. Thank you!

@lhotari
lhotari requested review from RobertIndie and nodece May 28, 2025 17:54
@nodece
nodece merged commit f673e4d into apache:master May 29, 2025
@RobertIndie RobertIndie added this to the v0.16.0 milestone Jul 29, 2025
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.

4 participants