Skip to content

feat: build logger messages lazily #3643

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

BobTheBuidler
Copy link

@BobTheBuidler BobTheBuidler commented Mar 13, 2025

What was wrong?

When using the logging module, we currently build the log message eagerly which leads to wasted cpu if the logger is not enabled.

According to DuckDuckGo's silly AI assistant:

"Using f-strings for logging in Python is generally discouraged because they evaluate the string immediately, which can lead to unnecessary computations even if the log level is not set to display that message. Instead, it's recommended to use the traditional formatting methods, like the % operator, which defer the string formatting until it's actually needed, improving performance and avoiding potential issues."

With this change, we will build the log messages lazily so the string is never built if the log will not actually be emitted, saving compute. This is just a microoptimization but will make a difference for power users such as myself who have use cases that require making a high volume of requests over a long period.

Related to Issue # N/A
Closes # N/A

How was it fixed?

replacing f-strings with strings which will be used with str.format

Todo:

  • Clean up commit history
  • Add entry to the release notes Not applicable for typical user so I skipped this addition

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

@BobTheBuidler BobTheBuidler marked this pull request as ready for review March 13, 2025 17:57
@BobTheBuidler BobTheBuidler changed the title feat: build logger strings lazily feat: build logger messages lazily Mar 13, 2025
@kclowes
Copy link
Collaborator

kclowes commented Mar 13, 2025

Thank you! Before merging, I'd like to make sure that this actually makes a 'meaningful' difference in performance before we sacrifice code consistency. Do you happen to know what magnitude of faster this is? If not, would you mind running a couple benchmarks?

@BobTheBuidler
Copy link
Author

BobTheBuidler commented Apr 5, 2025

This is on the backburner for now. I can tell you it saves about 33% of the time vs the existing code but I know you're going to want to see some data for this. I'm still going to put a benchmark together for that, when I get a chance. Stay tuned!

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