-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
AST-level Constant folding #73655
Comments
spin off of bpo-11549. |
I suggest you to look at my AST optimizer, especially the constant folding part: And the unit tests, search for BaseConstantFoldingTests: IHMO we must have a long test suite on this AST optimizer, because it's common that the AST changes in subtle ways, AST is complex and so we should prevent regressions. You may simply copy my unit tests. My optimizer implements more optimization: just remove unit tests on cases which you don't want to optimize. |
With this change, the Python compiler doesn't emit ast.Num nor ast.Str, right? If we merge such change, we should prepare projects using AST. There is something like that in pip, but I failed to remind which one :-/ It should be easy: apply your patch, try to install something using pip and see the traceback ;-) |
Do you mean https://github.com/pypa/pip/blob/403e398330c8e841e4633aceda859430f5f7b913/pip/_vendor/distlib/markers.py ? This doesn't affect, maybe. BTW, how do you feel asdl_ct.py? I feel it's too much only for constant folding, but it is too less for other advanced optimizations. I'll try to remove asdl_ct.py and ast_opt.ct in next version. |
In general the patch LGTM. I haven't found bugs. I think that at this stage we can asdl_ct.py and ast_opt.ct and use just their result, ast_opt.c (maybe rename to optimize.c?) The grammar is not often changed, and in that case it would be not hard to modify the code manually. In any case compile.c should be modified manually. After removing no-op functions like astfold_operator() and inlining simple functions like astfold_arg() the code should be clearer. Instead of converting all kinds of constants to Constant_kind, you could use is_const() and get_const_value() from compile.c. I suggest to remove the parts of the peepholer that are superseeded by the AST optimizer. As for preventing expensive calculations, there is a patch for the peepholer that does this. I wait on implementing the AST-level constant folding for adapting that patch for it. |
Naoki: Can you please create a PR for your patch? |
ISTM, the constant-tracking macros for peephole.c can also be removed, essentially reverting back to the simpler cumlc-style code in Py2.5. |
Thank you very much Naoki for taking time to implement this obvious "optimization", rewriting the peephole optimizer at the AST level, rather than modifying bytecode which is more complex to get it right and had annoying side effect (like inefficient stack size, now fixed: bpo-26549). |
PR 4866 also fixes the bug in optimizing chained 'i' and 'not in'. For example |
I tried it in #49129. |
docstring
field to AST nodesNote: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: