-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Duplicated unused bytecodes at end of function #86862
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
Comments
(Using CPython commit c95f8bc.) This program has extra bytecodes: def f():
for i in range(10):
break
return 17 The dis output is:
The break has something to do with it, because if I change the Python to: def f():
for i in range(10):
a = 1
return 17 then the dis output is:
|
My guess is that we are changing the CFG after computing reachability leaving unreachable blocks marked as reachable. |
Our reachability analysis is correct (in this case at least). In theory we could merge the two basic blocks at the end, but searching for identical blocks and merging them is potentially quite expensive (and fiddly). What might be better is to be change the order of optimizations, so that we duplicate BBs after we have performed jump-to-jump elimination. Is this a problem in practice? |
This isn't a problem for me. I noticed it and figured I'd mention it. |
It's just a hint that code generation has imperfections. From a teacher's or author's point of view, it is something we have to explain away when demonstrating dis(). It leaves the impression that Python is kludgy. |
In what way is it "kludgy"? 3.10a produces more efficient bytecode than 3.9. 3.9: 2 0 LOAD_GLOBAL 0 (range) 3 12 POP_TOP 4 >> 18 LOAD_CONST 2 (17) 3.10a: 2 0 LOAD_GLOBAL 0 (range) 3 12 POP_TOP 4 14 LOAD_CONST 2 (17) |
TL;DR: the The long story: The reason for the duplication is a small-exit-block-inlineing heuristic, where if we have an unconditional jump to an exit block of at most 4 instructions, that exit block is inlined and the jump is eliminated. I recently disabled this optimisation for the case where the exit block is associated with a line number, because it caused issues for the debugger (see #94592). For cases where the block doesn't have a line number (as in the case of the implicit Not duplicated:
Duplicated:
|
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: