-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-118761: Optimise import time for ast #131953
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
Conversation
And with this PR we are back down to 671 lines. The massive jump is from when unparse was added. This feels nice all on its own, to avoid so much code that is often not needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
A few thoughts:
- An alternative approach could be to make
ast
a package, with the unparse code inast/_unparse.py
and the rest of the functionality inast/__init__.py
. Pro: Doesn't need a new top-level module; avoids the (probably theoretical) compatibility concern that someone could have their own top-level_ast_unparse
module that interferes with the stdlib one. Con: Makes the directory structure more complex. - I'm not sure we need to keep getattr support for the private attributes, like the Precedence enum and the various constants. I'd do that only if there's evidence they are being used in third-party code, especially widely used packages. Otherwise we just make our lives harder if we want to refactor the unparse code in the future.
This reverts commit 460491e.
Reverted.
I'd suggest doing that in a follow-up PR, if desirable. I think for now this is the simplest approach. If it provides any reassurance, GitHub has no results for the |
Oh, I was reviewing the change while it has been merged :-) Well, it LGTM. I had minor suggestions, but they don't matter anymore. Nice optimization. |
Sorry! Happy to open a follow-up PR for any of your suggestions? |
Benchmark: 13.1 ms => 18.8 ms (-5.7 ms). Before:
After:
|
Well, I don't know if it would better, but an alternative to adding def unparse(ast_obj):
import _ast_unparse
return _ast_unparse.unparse(ast_obj) |
The |
Hmm, unless I am missing something, it seems that with this change ❯ python
Python 3.12.7 (main, Oct 1 2024, 00:00:00) [GCC 13.3.1 20240913 (Red Hat 13.3.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ast import *
>>> unparse
<function unparse at 0x7f7f8ebc9c60>
❯ ./python
Python 3.14.0a6+ (heads/main:f20f02e6b58, Apr 2 2025, 17:41:39) [GCC 13.3.1 20240913 (Red Hat 13.3.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ast import *
>>> unparse
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
unparse
NameError: name 'unparse' is not defined. Did you mean: 'parse'? |
I think (untested) that the |
I've opened #132024 for follow-ups. |
Alternative followup in #132025. |
Attempting @JelleZijlstra's suggestion in #118761 (comment).
Running
python -Ximporttime -Sc "import ast"
ten times with the current and proposed modules, I get 14.1ms for the current module and 1.48ms for the new, for a ~12ms speed up. I've left numbers out of the NEWS entry for now, as I've only tested on a Windows PC -- Linux may have different performance profiles.Full Times
(using the last line of each output)
Current
Proposed