Skip to content

gh-117865: Defer import of re in ast #119546

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

Merged
merged 2 commits into from
May 28, 2024
Merged

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented May 25, 2024

This is used only by ast.get_source_segment(), so it seems sensible to avoid importing it.
@AlexWaygood
Copy link
Member

How much does this speedup the import of inspect? I wouldn't expect it to do much on its own, since inspect itself also imports re. Or is the idea that this could be done in combination with some of the changes in #119526?

@AlexWaygood
Copy link
Member

(Though it's probably worth doing even if it just speeds up the import of ast. I guess I was looking at it through the lens of the github issue it was attached to.)

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Locally for me this cuts about 20% off the import time for ast (0.0036s -> 0.0029s), so this seems worth doing. This is especially true since the _splitlines_no_ff function is only ever used in get_source_segment, which is one of the lesser-used ast-module APIs

Co-authored-by: Alex Waygood <[email protected]>
@JelleZijlstra JelleZijlstra merged commit ae11d68 into python:main May 28, 2024
33 checks passed
@JelleZijlstra JelleZijlstra deleted the ast-re branch May 28, 2024 18:04
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
This is used only by ast.get_source_segment(), so it seems sensible to avoid importing it.

Co-authored-by: Alex Waygood <[email protected]>
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.

2 participants