GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-109653: Improve import time of `logging` by lazy loading `traceback` by danielhollas · Pull Request #112995 · python/cpython

Conversation

@danielhollas

@danielhollas danielhollas commented Dec 12, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.

  • Issue: Improve import time of various stdlib modules #109653

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
danielhollas added 2 commits December 10, 2023 02:29
@danielhollas
Speed up import time of logging module
6c4e173
Delayed import of traceback results in ~16% speedup.
Issue python#109653
@danielhollas
Add NEWS entry
5ae66a2
@ghost

ghost commented Dec 12, 2023 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot mentioned this pull request Dec 12, 2023
Improve import time of various stdlib modules #109653
Closed
@jgeralnik

jgeralnik commented Dec 12, 2023

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)

danielhollas reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@danielhollas

danielhollas commented Dec 12, 2023

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?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@danielhollas

danielhollas commented Dec 14, 2023

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@danielhollas

danielhollas commented Jan 24, 2024

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@danielhollas danielhollas closed this Jan 24, 2024
@AlexWaygood

AlexWaygood commented Jan 24, 2024

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 reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@danielhollas
danielhollas deleted the import-logging-speedup branch January 24, 2024 15:24
@danielhollas

danielhollas commented Jan 24, 2024

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

AlexWaygood reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@danielhollas danielhollas mentioned this pull request Feb 2, 2026
_colorize module is slow to import, affecting traceback and logging modules #144384
Open
@danielhollas danielhollas mentioned this pull request May 20, 2026
gh-150072: Optimize logging imports by lazily importing traceback #150073
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

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

Labels

None yet

Projects

None yet

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.

3 participants

@danielhollas @jgeralnik @AlexWaygood
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.