Skip to content
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

bpo-42246: Eliminate jumps to exit blocks by copying those blocks. #23251

Merged

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Nov 12, 2020

More yak-shaving for PEP 626.

This PR:

  • Marks all blocks that exit the scope or that do not fall through to the b_next block.
  • Removes the a_reverse_postorder array in the assembler, as it is just a copy of the b->b_next chain.
  • Eliminates unconditional jumps to short exit blocks, by copying the exit blocks onto the end of its predecessor.

The net effect of this is that:

def f(c):
    if c:
        A
    else:
        B

now compiles to:

  2           0 LOAD_FAST                0 (c)
              2 POP_JUMP_IF_FALSE       12

  3           4 LOAD_GLOBAL              0 (A)
              6 POP_TOP
              8 LOAD_CONST               0 (None)
             10 RETURN_VALUE

  5     >>   12 LOAD_GLOBAL              1 (B)
             14 POP_TOP
             16 LOAD_CONST               0 (None)
             18 RETURN_VALUE

instead of

  2           0 LOAD_FAST                0 (c)
              2 POP_JUMP_IF_FALSE       10

  3           4 LOAD_GLOBAL              0 (A)
              6 POP_TOP
              8 JUMP_FORWARD             4 (to 14)

  5     >>   10 LOAD_GLOBAL              1 (B)
             12 POP_TOP
        >>   14 LOAD_CONST               0 (None)
             16 RETURN_VALUE

This has two effects

  1. Means that f_lineno can be determined from f_lasti even after an implicit return. This is needed for PEP 626.
  2. Reduces the number of bytecodes executed at the cost of a minor increase in bytecode size, and might result in a tiny speedup. I haven't measured it, as I expect the change to be too small to meaningful, or even measurable.

https://bugs.python.org/issue42246

@markshannon markshannon force-pushed the compiler-eliminate-jump-to-exits branch from 1930bc7 to 0d2af11 Compare November 12, 2020 16:02
@markshannon markshannon merged commit cc75ab7 into python:master Nov 12, 2020
@bedevere-bot
Copy link

@markshannon: Please replace # with GH- in the commit message next time. Thanks!

adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
…ython#23251)

* Compiler: eliminate jumps to short exit blocks by copying.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants