FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-109653: Improve import time of `logging` by lazy loading `traceback` by danielhollas · Pull Request #112995 · python/cpython · GitHub

/ cpython Public

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

Closed
danielhollas wants to merge 2 commits into
python:mainfrom
danielhollas:import-logging-speedup
Closed

gh-109653: Improve import time of logging by lazy loading traceback#112995
danielhollas wants to merge 2 commits into
python:mainfrom
danielhollas:import-logging-speedup

Conversation

danielhollas commented Dec 12, 2023
edited
Loading

Copy link
Copy Markdown
Contributor

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 commented Dec 12, 2023
edited by ghost
Loading

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

Copy link
Copy Markdown

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)

Copy link
Copy Markdown
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?

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
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

Copy link
Copy Markdown
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 deleted the import-logging-speedup branch January 24, 2024 15:24

Copy link
Copy Markdown
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 :-) )

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL