Source

astoptimizer / TODO

Full commit
TODO
====

bugs:

 - "len=str; print(len(1))". Need to support variables.
 - "i=0; while i < 10: print(i); i = i + 1": don't replace print(i) with print('0')

major optimizations:

 - move invariant out of the loop: "x=[0]; for ...: x.append(...)"
   becomes "x=[0]; x_append=x.append; for ...: x_append(...)"
 - drop dead code with a warning: "x=1; 'useless instruction'; print(x)"

   * "x=1; print(x)" => "print('1')" (drop x)
   * "x=1" drops x
   * "return x" keeps x

 - drop unused instructions with a warning: "def f(): x=1; return 2"
 - support variables: x="abc"; print(len(x))

   * rewrite the optimizer to work in two steps:

     a) scan function arguments, variables and builtins; detect usage of globals()
     b) replace constants, remove unused variables, peephole, call builtin functions

   * support scopes: module, functiondef, classdef, nested def, ...

 - unroll short loops: "for x in range(4): print(x)"

   * duplicate the body and evaluate the body with x=0, x=1, ...
   * handle continue/break
   * drop x if possible

 - convert naive loop to list comprehension: "x=[]; for item in data: x.append(item.upper())"
   => "x=[item.upper() for item in data]" ?

 - inline functions: see http://bugs.python.org/issue10399

other:

 - [x for x in range(3)] => xrange
 - from re import *; from math import pow; print(pow(2, 4)) => 16.0
 - from re import * => test: check for warning
 - enable pythonbin by default?
 - not not not x
 - 1 < 2 < 3
 - x in [1, 2, 3] => x in (1, 2, 3)
 - x in {1, 2, 3} => x in frozenset((1, 2, 3))
 - str.format() (str%args already implemented), don't optimize {!n} (locale dependant)
 - logging machinery: "from unknown import *" => WARNING
 - write tests for patch_compile(), ex: ast.literal_ast("1+(2+3j)")
 - optimize using the major Python version? ord("\U0010ffff") for Python 3.3
 - infer type
 - (x * 2) * 3 => x * 6, "fast-math" option?

later:

 - re module, warning at the locale dependent patterns
 - base64, binascii
 - pow() and a**b for complex numbers
 - optimize mutable types? "x=[1, 2, 3]; print(len(x))"
 - Python3: print(sep="-"); print(end="")