constant folding - why not more

David Kolovratník david at kolovratnik.net
Tue Nov 10 09:45:01 EST 2020


Dear all,

I would like to learn about constant folding optimisation in Python. It seems
to be implemented in Python/ast_opt.c. In order to get impression I used
python3 and dis module:

$ python3 -V
Python 3.7.3

Arithmetics expression is folded as expected:

>>> dis.dis(compile('1 * 2', filename='<string>', mode='eval',
>>> optimize=2))
  1           0 LOAD_CONST               0 (2)
              2 RETURN_VALUE

On the contrary, comparison remains for runtime:
>>> dis.dis(compile('1 < 2', filename='<string>', mode='eval',
>>> optimize=2))
  1           0 LOAD_CONST               0 (1)
              2 LOAD_CONST               1 (2)
              4 COMPARE_OP               0 (<)
              6 RETURN_VALUE
In function fold_unaryop (though comparison is a binary operation) in
Python/ast_opt.c is remark: /* Fold not into comparison */

Is there a reason why comparison (== != < > <= >=) is not folded? I would
expect it handled in fold_binop.

Besides comparison there are other expressions that might be optimized out 
due to constant expression. Ternary operator with constant condition True
has IF optimized out (just some dead code remains):
>>> dis.dis(compile('"a" if True else "b"', filename='<string>',
>>> mode='eval', optimize=2))
  1           0 LOAD_CONST               1 ('a')
              2 RETURN_VALUE
              4 LOAD_CONST               2 ('b')
              6 RETURN_VALUE

On the contrary, the same ternary operator with constant condition False
still loads the constat and contains POP_JUMP_IF_FALSE:
>>> dis.dis(compile('"a" if False else "b"', filename='<string>',
>>> mode='eval', optimize=2))
  1           0 LOAD_CONST               0 (False)
              2 POP_JUMP_IF_FALSE        8
              4 LOAD_CONST               1 ('a')
              6 RETURN_VALUE
        >>    8 LOAD_CONST               2 ('b')
             10 RETURN_VALUE

Any ideas or links, please?

Best regards,
Davis


More information about the Python-list mailing list