astoptimizer is a module that optimizes the Python code working on AST (Abstract Syntax Tree) high-level representration.
Optimizations
- Call builtin functions if arguments are constants. Examples:
len("abc") => 3
ord("A") => 65
- Call methods of builtin types if the object and arguments are constants. Examples:
u"h\\xe9ho".encode("utf-8") => b"h\\xc3\\xa9ho"
"python2.7".startswith("python") => True
(32).bit_length() => 6
float.fromhex("0x1.8p+0") => 1.5
- Call functions of math and string modules for functions without border effect. Examples:
math.log(32) / math.log(2) => 5.0
string.atoi("5") => 5
- Format strings for str%args and print(arg1, arg2, ...) if arguments are constants and the format string is valid. Examples:
"x=%s" % 5 => "x=5"
print(1.5) => print("1.5")
- Simplify expressions. Examples:
not(x in y) => x not in y
4 and 5 and x and 6 => x and 6
- Loop: replace range() with xrange() on Python 2, and list with tuple. Examples:
for x in range(n): ... => for x in xrange(n): ...
for x in [1, 2, 3]: ... => for x in (1, 2, 3): ...
- Evaluate unary and binary operators, subscript and comparaison if all arguments are constants. Examples:
1 + 2 * 3 => 7
not True => False
"abc" * 3 => "abcabcabc"
abcdef[:3] => abc
(2, 7, 3)[1] => 7
frozenset("ab") | frozenset("bc") => frozenset("abc")
None is None => True
"2" in "python2.7" => True
"def f(): return 2 if 4 < 5 else 3" => "def f(): return 2"
- Remove dead code. Examples:
def f(): return 1; return 2 => def f(): return 1
if DEBUG: print("debug") => pass with DEBUG declared as False
while 0: ... => pass
Product's homepage
Requirements:
· Python
Limitations:
· Operations on mutable values are not optimized, ex: len([1, 2, 3]).
· Unsafe optimizations are disabled by default. For example, len("\U0010ffff") is not optimized because the result depends on the build options of Python. Enable "builtin_funcs" and "pythonenv" features to enable more optimizations.
· len() is not optimized if the result is bigger than 2^31-1. Enable "pythonbin" configuration feature to optimize the call for bigger objects.
· On Python 2, operators taking a bytes string and a unicode string are not optimized if the bytes string has to be decoded from the default encoding or if the unicode string has to be encoded to the default encoding. Exception: pure ASCII strings are optimized. For example, b"abc" + u"def" is replaced with u"abcdef", whereas u"x=%s" % b"\xe9" is not optimized.
· On Python 3, comparaison between bytes and Unicode strings are not optimized because the comparaison may emit a warning or raise a BytesWarning exception. Bytes string are not converted to Unicode string. For example, b"abc" < "abc" and str(b"abc") are not optimized. Converting a bytes string to Unicode is never optimized.
What's New in This Release: [ read full changelog ]
· Disable optimizations on functions and constants if a variable with the same name is set. Example: "len=ord; print(len('A'))", "sys.version = 'abc'; print(sys.version)".
· Don't optimize print() function, frozenset() nor range() functions if "builtin_funcs" feature is disabled
· Don't remove code if it contains global or nonlocal. Example: "def f(): if 0: global x; x = 2".