-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
Peeephole optimizer does not optimize functions with multiline expressions #81394
Comments
The optimization is skipped if lnotab contains 255. It was very uncommon in older versions (only when the function contains very large expressions, larger than hundreds of lines or bytecode instructions), but in 3.8 this situation is common. For example: [x 1 0 BUILD_LIST 0 2 6 STORE_FAST 1 (x) 1 12 LOAD_FAST 1 (x) if x:
if (y and
z):
foo()
else:
bar() 1 0 LOAD_NAME 0 (x) 2 4 LOAD_NAME 1 (y) 3 8 LOAD_NAME 2 (z) 2 10 POP_JUMP_IF_FALSE 18 4 12 LOAD_NAME 3 (foo) 6 >> 20 LOAD_NAME 4 (bar) You can see non-optimized jumps to jumps (from 10 to 16 and from 6 and 10 to 16 correspondingly). This is a consequence of two features: ability to encode negative line differences in lnotab and setting lines for both outer and inner expressions. Two ways to solve this issue:
Pablo, are you interesting? |
Thank you very much, Serhiy! I am interested, I will try to look at the problem and try to get a PR soon. What of the two possible solutions that you mention you think is better? I assume if we make the peephole optimizer work with lnotab containing 255 we could backport to 3.8 as a bugfix, right? |
Yes, we should backport the fix to 3.8. There is a bug in 3.8. |
I removed the memchr(255) to see which tests fail: test_extended_opargs (test.test_modulefinder.ModuleFinderTest) ... python: Python/peephole.c:469: PyCode_Optimize: Assertion `cum_orig_offset % sizeof(_Py_CODEUNIT) == 0' failed. test_extended_arg (test.test_compile.TestSpecifics) ... python: Python/peephole.c:469: PyCode_Optimize: Assertion `cum_orig_offset % sizeof(_Py_CODEUNIT) == 0' failed. test_field_named_like_builtin (test.test_dataclasses.TestCase) ... python: Python/peephole.c:469: PyCode_Optimize: Assertion `cum_orig_offset % sizeof(_Py_CODEUNIT) == 0' failed. test_field_named_like_builtin_frozen (test.test_dataclasses.TestCase) ... python: Python/peephole.c:469: PyCode_Optimize: Assertion `cum_orig_offset % sizeof(_Py_CODEUNIT) == 0' failed. Does test_compile have unit tests test_modulefinder and test_dataclasses cases? |
Do you know why 255 became more common? Is it the side effect if an AST optimization? |
Should we backport this to 3.7 as well? |
Not unless someone can show how this is a major problem in 3.7 and then only if the changes will not introduce any 3.7.x compatibility problems. |
I would say is not a major problem in 3.7 I will close the issue then. Thanks everyone who participated! |
Because the line number is now correctly set for every bytecode instruction. Compare the output in msg345108 for 3.8 with the corresponding output in 3.7: 1 0 BUILD_LIST 0 2 6 STORE_FAST 1 (x) |
That's a great enhancement! Should it be documented in https://docs.python.org/3.8/whatsnew/3.8.html ? |
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: