python new AST optimizer

Viewed 388

from python 3.7 what's new

Constant folding has been moved from the peephole optimizer to the new AST optimizer, which is able perform optimizations more consistently

what optimizations is this new AST optimizer able to perform and how is it different from the peephole optimizer ?

1 Answers

If you read the associated bug reports, they contain most of the details. Most of it is pretty dry stuff that doesn't really matter to most code, which is why they don't include the details in "What's New".

Examples where it matters are usually strange cases that are rarely encountered, but that the peephole optimizer (which couldn't do higher level evaluation, just simple byte code rewrites in place) couldn't handle, e.g. on Python 3.6:

>>> import dis
>>> dis.dis('if True: pass')  # Recognizes simple case and omits whole block
  1           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE
>>> dis.dis('if True and 1: pass')  # Can't recognize more complex case
  1           0 LOAD_CONST               0 (True)
              2 POP_JUMP_IF_FALSE        8
              4 LOAD_CONST               1 (1)
              6 POP_JUMP_IF_FALSE        8
        >>    8 LOAD_CONST               2 (None)
             10 RETURN_VALUE

whereas on 3.7:

>>> import dis
>>> dis.dis('if True: pass')  # Recognizes simple case and omits whole block
  1           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE
>>> dis.dis('if True and 1: pass')  # Handles more complex case too
  1           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE
Related