[Python-Dev] AST optimizer implemented in Python

Victor Stinner victor.stinner at gmail.com
Sun Aug 12 03:17:54 CEST 2012


>> 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.

For this specific example, x.append cannot be modified: it raises
AttributeError('list' object attribute 'append' is read-only).

The idea would be to allow the developer to specify explicitly what he
wants to optimize. I'm using a configuration class with a list of what
can be optimized (ex: len(int)), but it can be changed to something
different later.

It must be configurable to be able to specify: "this specific variable
is constant in my project"..

> Perhaps the best way is to hide potentially-risky optimizations behind
> command-line options? The default mode could be to do every change
> that's guaranteed not to affect execution, and everything else is an
> extra (like French, music, and washing).

I don't care of integration into Python yet (but it would be nice to
prepare such feature in Python 3.4). It can be a third party module,
something like:

   import ast_optimizer
   ast_optimizer.hack_import_machinery()
   ast_optimizer.constants.add('application.DEBUG')

(added on the top of your main script)

Victor


More information about the Python-Dev mailing list