Skip to content

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

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 1 commit into from

Conversation

mdboom
Copy link
Contributor

@mdboom mdboom commented Apr 25, 2025

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 mdboom changed the title gh-127648: Improve Python startup time by ~12% gh-132952: Improve Python startup time by ~12% Apr 25, 2025
@JelleZijlstra
Copy link
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.

@sergey-miryanov
Copy link
Contributor

I believe it is related to #118761

Copy link
Member

@AA-Turner AA-Turner left a comment

Choose a reason for hiding this comment

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

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

A

@mdboom
Copy link
Contributor Author

mdboom commented Apr 26, 2025

@AA-Turner
Copy link
Member

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

A

@JelleZijlstra
Copy link
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.

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

mdboom commented Apr 28, 2025

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