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
If without else generates redundant jump #55680
Comments
If statement without else part generates unnecessary JUMP_FORWARD insn with jumps right to the next insn: >>> def foo(x):
if x: x = 1
>>> dis(foo)
2 0 LOAD_FAST 0 (x)
3 POP_JUMP_IF_FALSE 15
6 LOAD_CONST 1 (1)
9 STORE_FAST 0 (x)
12 JUMP_FORWARD 0 (to 15)
>> 15 LOAD_CONST 0 (None)
18 RETURN_VALUE This patch suppresses generation of this jump. Testing revealed another issue: when AST is produced from string empty 'orelse' sequences are represented with NULLs. However when AST is converted from Python AST objects empty 'orelse' is a pointer to 0-length sequence. I've changed this to produce NULL pointers, like in the string case. This uses less memory and doesn't introduce different code path in compiler. Without this change test_compile failed with my first change. make test passes. |
Test case (needed some refactoring to avoid duplication). |
Python-ast.c can't be changed; it is auto-generated. But the whole thing can be handled in compile.c I think -- see attached patch. Test suite passes (except for test_dis, which checks compilation result against a given list of bytecodes). |
I'll make a complete patch with test suite additions (and fixing test_dis) if this is deemed to be a correct approach. |
I think it's fine with the simplification I suggested. |
Updated patch with test suite update. |
lgtm |
Just for the record, have you passed the whole test suite after cleaning up the pyc files, |
Yep :) |
LGTM. Attila Fazekas just has provided almost the same patch in bpo-22358. |
New changeset c0ca9d32aed4 by Antoine Pitrou in branch 'default': |
Pushed after applying Serhiy's suggestion. Thank you! |
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: