GeezerVison is showing github.com — gh-109653: Improve import time of `logging` by lazy loading `traceback` by danielhollas · Pull Request #112995 · python/cpython
Conversation
Lazy importing traceback module in logging improves the import time by ~15% on my machine (PGO-optimized build on Linux). Since traceback is used here to handle exceptions, these code paths should by definition better be exceptional so lazy loading makes sense.
In order to avoid performance impacts, I have used a trick proposed in a similar PR. Perhaps this trick is not necessary here though since as mentioned above these code paths should not be hot.
TODO: Fix the failing test.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Delayed import of traceback results in ~16% speedup.
Issue python#109653
ghost
commented
Dec 12, 2023
•
edited by ghost
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
All commit authors signed the Contributor License Agreement. 
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
The tests fail because you can't import traceback when python is shutting down (but logging can still be called - e.g. in a del function of a global object)
|
danielhollas reacted with heart emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@jgeralnik oh, interesting, thank you! That would take me some time to figure out. So what you're saying is that this particular PR is doomed, we simply can't import traceback lazily? Or do you see some way around it?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@AlexWaygood this ended up being harder than I thought, see comments above. Unless you have any suggestions I'll close this.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
The tests fail because you can't import traceback when python is shutting down (but logging can still be called - e.g. in a del function of a global object)
It seems that new modules can't be imported during Python shutdown, and the logging module needs to be available during shutdown so I don't see a way around this. Closing.
CC @AlexWaygood
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
^Sorry for not getting to this earlier! Been unusually busy the last month or so :)
Yeah, the stuff that goes on when Python is shutting down is... scary. Agree with the decision to close this one :)
|
danielhollas reacted with heart emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@AlexWaygood no worries at all, thanks for the comment and review on #114509 🤝
It was interesting to poke at stdlib for the first time (and realizing, as always, that things are more complicated than originally thought :-) )
|
AlexWaygood reacted with heart emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
vsajip
Awaiting requested review from vsajip
vsajip will be requested when the pull request is marked ready for review
vsajip is a code owner
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|