gh-156777: Optimize import overhead in the logging package - #156778
Open
brittanyrey wants to merge 4 commits into
Open
gh-156777: Optimize import overhead in the logging package#156778brittanyrey wants to merge 4 commits into
brittanyrey wants to merge 4 commits into
Conversation
Lift the function-level imports in logging/handlers.py and logging/config.py
to module scope as lazy imports, and mark other cold-path imports lazy.
Imports reachable from interpreter finalization stay eager and are now
commented as such: at teardown sys.modules has been cleared, so resolving a
lazy import raises ImportError('sys.meta_path is None').
The Windows-only (_winapi, winreg) and optional third-party
(win32evtlogutil) imports stay function-level on purpose: as module-level
lazy imports they would break inspect.getmembers() and pydoc on non-Windows.
import logging.config -28.3%
import logging.handlers -17.8%
import logging unchanged
Call-time benchmarks unchanged (all within noise).
eendebakpt
reviewed
Sep 1, 2026
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
eendebakpt
reviewed
Sep 1, 2026
hugovk
reviewed
Sep 1, 2026
Sort each import block into the isort/Ruff groups requested in review: plain imports, from-imports, lazy imports, lazy from-imports. In logging/config.py, replace ``lazy import logging.handlers`` with ``lazy from logging import handlers``. The former binds the name ``logging`` lazily, so it shadows the eager ``import logging`` above it and any use of ``logging.<anything>`` in the module pulls in logging.handlers. Aliased to ``logging_handlers`` because ``handlers`` is already a local variable in _install_handlers() and _configure_queue_handler(). Now logging.handlers is imported only when a handler is configured through the ``class`` key; previously dictConfig(), fileConfig() and stopListening() all reified it. Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com> Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
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.
summary
perf
import logging.configimport logging.handlersimport logging