gh-150818: Speed up logging.getLogger() for existing loggers#150825
Open
gaborbernat wants to merge 1 commit into
Open
gh-150818: Speed up logging.getLogger() for existing loggers#150825gaborbernat wants to merge 1 commit into
gaborbernat wants to merge 1 commit into
Conversation
getLogger() took the logging lock on every call, including the common case of an already-registered logger. Return that logger through a lock-free fast path backed by an atomic dict lookup. First-time creation, placeholder resolution and parent/child wiring still run under the lock, and the fast path is safe under both the GIL and free threading.
defd276 to
9c0df96
Compare
sobolevn
reviewed
Jun 3, 2026
| rv = self.loggerDict.get(name) | ||
| if rv is not None and not isinstance(rv, PlaceHolder): | ||
| return rv | ||
| rv = None |
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.
logging.getLogger(name)returns the singleton logger for a name, creating it on first use. Every call takes the logging lock, even the overwhelmingly common case where the logger already exists and is returned unchanged. Libraries fetch their logger by name throughout their code, at module import and again inside functions, so the same handful of names are looked up over and over for the life of a process. Those repeat lookups are pure overhead: the logger is already there.This returns an existing, fully-initialised logger through a lock-free fast path before falling back to the locked section. The fast path reads
loggerDictwith a singledict.get(), which is atomic under both the GIL and free threading, and aLoggeris inserted into that dict only after it is fully constructed under the lock, so the fast path never observes a half-built object or a placeholder. First-time creation, placeholder resolution and the parent/child wiring still run under the lock exactly as before.Resolving logger names collected from the top-1000 corpus improves from 6.68 µs to 5.02 µs, 33% faster.
Benchmark (pyperf)
Run base vs patched by swapping
Lib/logging/__init__.pyon the same interpreter. The names are realgetLogger()string arguments mined from the top-1000 corpus.Resolves #150818.