Skip to content

gh-109653: Improve import time of logging by lazy loading traceback #112995

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

Closed
wants to merge 2 commits into from

Conversation

danielhollas
Copy link
Contributor

@danielhollas danielhollas commented Dec 12, 2023

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.

Delayed import of traceback results in ~16% speedup.
Issue python#109653
@ghost
Copy link

ghost commented Dec 12, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@jgeralnik
Copy link

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
Copy link
Contributor Author

@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?

@danielhollas
Copy link
Contributor Author

@AlexWaygood this ended up being harder than I thought, see comments above. Unless you have any suggestions I'll close this.

@danielhollas
Copy link
Contributor Author

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

@AlexWaygood
Copy link
Member

^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 danielhollas deleted the import-logging-speedup branch January 24, 2024 15:24
@danielhollas
Copy link
Contributor Author

@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 :-) )

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.

3 participants