[issue1346238] A constant folding optimization pass for the AST

Paul Sokolovsky report at bugs.python.org
Wed Jan 1 05:51:49 CET 2014


Paul Sokolovsky added the comment:

8 years after the original patch, there's still no trivial constant folding in bytecode generated (because peephole of course is not a real optimizer to consistently catch all cases):

$ cat const.py 
FOO = 1
BAR = FOO + 2 + 4

$ python --version
Python 2.7.3

$ python -OO -m dis const.py
  1           0 LOAD_CONST               0 (1)
              3 STORE_NAME               0 (FOO)

  2           6 LOAD_NAME                0 (FOO)
              9 LOAD_CONST               1 (2)
             12 BINARY_ADD          
             13 LOAD_CONST               2 (4)
             16 BINARY_ADD          
             17 STORE_NAME               1 (BAR)
             20 LOAD_CONST               3 (None)
             23 RETURN_VALUE        

$ python3.3 --version
Python 3.3.3

$ python3.3 -OO -m dis const.py
  1           0 LOAD_CONST               0 (1) 
              3 STORE_NAME               0 (FOO) 

  2           6 LOAD_NAME                0 (FOO) 
              9 LOAD_CONST               1 (2) 
             12 BINARY_ADD           
             13 LOAD_CONST               2 (4) 
             16 BINARY_ADD           
             17 STORE_NAME               1 (BAR) 
             20 LOAD_CONST               3 (None) 
             23 RETURN_VALUE

----------
nosy: +pfalcon

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue1346238>
_______________________________________


More information about the Python-bugs-list mailing list