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
Remove unneeded folded consts after peephole #72999
Comments
The attached patch adds new logic to peephole compiler to remove constants that are no longer needed after the main peephole pass. For example: def f():
var = 'te' + 'xt'
num = -12
num = -6 * 2
return (1, (3, 4), 6)
print(f.__code__.co_consts) Without the patch:
With patch:
(unfortunately, I couldn't get rid of None because that would make 'text' a docstring) For convenience, I've written the patch in two parts. Aside from simply having to store less objects around, this also makes co_consts contents closer together. This may help the cache a little bit. --------- I did run benchmarks multiple times, but it looked like all the results were random noise. That makes sense, since I didn't directly affect the runtime.
I tried to measure the difference in compile time, but it too was lost in the noise. --------- I also compared size of compiled .pyc files in the Lib/ directory. _compat_pickle.cpython-37.pyc | 6554 -> 5851 | -10.7% |
Thank you for your patch Adrian. I haven't close look, but at first glance your patch looks correct, and the idea looks great. But moving constant folding from the peephole optimizer to the AST level (bpo-1346238, bpo-11549) would totally eliminate the need in your patch. I'll push your patch if AST optimizer will be not implemented in 3.7. On other hand, your patch looks simple enough, and my be pushed first. It would be easy to review if provide your changes as one patch. |
Attached squashed patch.
I'm aware of that and I'm okay with it. I chose an unfortunate moment for implementing this :) |
FWIW, we intentionally decided not to do this when constant folding was added. The idea was to keep the peephole optimizer simple and to have it do the minimum work necessary to get its job done (optimizing the constants table takes extra time to do but doesn't result in faster code). Another reason was that aside from contrived examples (such as the OP's example), very little real-world code gets any benefit and the benefit tends to be very small. (In other words, no one will actually notice or benefit from this patch, but their compilation times will all slow down slightly). Lastly, the intention is to stop building out constant folding. The correct place for constant folding is upstream, using AST prior to code generation. |
Moving constant folding to the AST level (bpo-29469) have made this patch outdated. In any case thank you for your patch Adrian. |
Note: 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: