[issue21074] Too aggressive constant folding

STINNER Victor report at bugs.python.org
Thu Mar 27 09:18:41 CET 2014


STINNER Victor added the comment:

Hi, I have a project called "astoptimizer" which does the same job than the CPython peephole optimizer, but it is implemented in Python.

To avoid this issue (create an huge object and then discard it because it is too large), I have a "check_binop_cst" function which checks if the result will fit into the configuration constraints:

https://bitbucket.org/haypo/astoptimizer/src/024c05ae410458813c20fafe8fe5b8d3cf980f52/astoptimizer/optimizer.py?at=default#cl-598

Check for "a * b" :
-------------
        if isinstance(op, ast.Mult):
            if isinstance(right, INT_TYPES):
                # str * int
                if isinstance(left, STR_TYPES):
                    return (len(left) * right <= self.config.max_string_length)
                # tuple * int
                if isinstance(left, tuple):
                    return (len(left) * right <= self.config.max_tuple_length)

            if isinstance(left, INT_TYPES):
                # int * str
                if isinstance(right, STR_TYPES):
                    return (left * len(right) <= self.config.max_string_length)
                # int * tuple
                if isinstance(right, tuple):
                    return (left * len(right) <= self.config.max_tuple_length)
-------------

Constraints in the config:

https://bitbucket.org/haypo/astoptimizer/src/024c05ae410458813c20fafe8fe5b8d3cf980f52/astoptimizer/config.py?at=default#cl-187

Astoptimizer supports the following "constant" types: int, float, complex, bytes, str, tuple, "name constant" (True, False, None). The frozenset type is also supported if the builtins are declared as "cosntant" too.

----------

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


More information about the Python-bugs-list mailing list