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

gh-132952: Improve Python startup time by ~12% by mdboom · Pull Request #132956 · python/cpython · GitHub

Repository navigation

gh-132952: Improve Python startup time by ~12% - #132956

Closed
mdboom wants to merge 1 commit into
python:mainfrom
mdboom:python-startup-time
Closed

mdboom wants to merge 1 commit into
python:mainfrom
mdboom:python-startup-time

Conversation

mdboom commented Apr 25, 2025 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

This improves the time of a no-site.py startup by 12%, i.e.

python -c pass -S

I'm not strongly of the opinion this should be merged (hence the draft PR), since there are probably all kinds of other places where "lazy importing" could be used to improve startup time that don't improve readability. On the other hand, it's a fairly large regression from 3.13.x that we may want to avoid.

mdboom changed the title gh-127648: Improve Python startup time by ~12% gh-132952: Improve Python startup time by ~12% Apr 25, 2025

Copy link
Copy Markdown
Member

This only helps the -S startup because site.py imports os.py which also imports _collections_abc.

I think it's pretty defensible to lazy-import here as well as in os.py though; the import is only needed for runtime isinstance() checks on these protocols, and many users will use them without ever needing the isinstance() call.

Copy link
Copy Markdown
Contributor

I believe it is related to #118761

AA-Turner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Especially as the intent is for these to be Protocol classes, I agree with Jelle that deferring makes sense.

A

mdboom commented Apr 26, 2025

Copy link
Copy Markdown
Contributor Author

Copy link
Copy Markdown
Member

@mdboom are you happy to merge this? It's still 'draft'.

A

Copy link
Copy Markdown
Member

If we merge this, let's try to make it so _collections_abc isn't imported on normal (without -S) startup too, so this helps in a bit more realistic cases. It should be easy to defer the import in os.py.

Copy link
Copy Markdown
Member

It turns out it's not actually feasible to make os.py also lazy-import _collections_abc, because os.Environ inherits from _collections_abc.MutableMapping.

That means (if we merge #132957) this PR only helps in the case where the user used -S and imports io, but not os or pretty much anything else. I feel that's unlikely enough that it's not worth making this change.

mdboom commented Apr 28, 2025

Copy link
Copy Markdown
Contributor Author

It turns out it's not actually feasible to make os.py also lazy-import _collections_abc, because os.Environ inherits from _collections_abc.MutableMapping.

That means (if we merge #132957) this PR only helps in the case where the user used -S and imports io, but not os or pretty much anything else. I feel that's unlikely enough that it's not worth making this change.

Yeah, I agree. I think #132957 is the better solution. Closing.

mdboom closed this Apr 28, 2025
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL