[Python-Dev] Release of astoptimizer 0.3

Victor Stinner victor.stinner at gmail.com
Tue Sep 11 23:47:40 CEST 2012

>>>   - frozenset("ab") | frozenset("bc") => frozenset("abc")
> That's a name lookup, too.

Yes, and it is only optimized if the "builtin_funcs" feature is
enabled explictly.

> Except that evaluating something like '"abc" * constant' can eat up all
> memory, imagine this code:
>     KILL_MEMORY = sys.argv[1] == 'NEVER PASS THIS VALUE'
>     def test():
>         if KILL_MEMORY:
>             return 'abc' * 10000000000000000000000000000
>         else:
>             return 'abc'

The optimizer always check the result of an optimization for
constraints. For example, an optimization is cancelled if the result
is not immutable. Integers must be in the range [-2^128; 2^128-1] by
default. Strings are limited to 4096 bytes/characters. Tuples are
limited to 20 items.

The compiler may waste some seconds to evaluate an expression and then
drop the result, but I don't really care right now. I prefer to spend
minutes to compile if the application is faster at runtime.

astoptimizer tries to avoid expensive operations when it knows that
the result will be too big. For your specific example, str * int is
*not* evulated if the result will be longer than the limit. Similar
sanity checks may be added later for pow(int, int) and
math.factorial(int) for example.


More information about the Python-Dev mailing list