[Python-Dev] AST optimizer implemented in Python

Stefan Behnel stefan_ml at behnel.de
Sun Aug 12 06:50:20 CEST 2012


Stefan Behnel, 12.08.2012 06:42:
> Chris Angelico, 12.08.2012 01:22:
>>> Other idea to improve this optimizer:
>>>  - move invariant out of loops. Example: "x=[]; for i in range(10):
>>> x.append(i)" => "x=[]; x_append=x.append; for i in range(10):
>>> x_append(i)". Require to infer the type of variables.
>>
>> But this is risky. It's theoretically possible for x.append to replace
>> itself. Sure it may not be a normal or common thing to do, but it's
>> possible.
> 
> Not only that. It changes semantics. If x.append is not defined, the
> exception would now be raised outside of the loop, and the loop itself may
> have side-effects already. In fact, the mere lookup of x.append may have
> side effects as well ...

That being said, the specific case above can be optimised, we do something
like this in Cython, too. It requires both (simple) type inference and
control flow analysis, though, because you need to know that a) x holds a
list and b) it was assigned outside of the loop and is not being assigned
to inside. So it might look simple and obvious, but it requires quite a bit
of compiler infrastructure.

Stefan




More information about the Python-Dev mailing list