<div dir="ltr"><div>Hi,<br>
<br>
I'm writing a new "FAT Python" project to try to implement optimizations in CPython (inlining, constant folding, move invariants out of loops, etc.) using a "static" optimizer (not a JIT). For the background, see the thread on python-ideas:<br>
<a href="https://mail.python.org/pipermail/python-ideas/2015-October/036908.html" target="_blank">https://mail.python.org/pipermail/python-ideas/2015-October/036908.html</a><br><br></div>See also the documentation:<br>
<a href="https://hg.python.org/sandbox/fatpython/file/tip/FATPYTHON.rst" target="_blank">https://hg.python.org/sandbox/fatpython/file/tip/FATPYTHON.rst</a><br><a href="https://hg.python.org/sandbox/fatpython/file/tip/ASTOPTIMIZER.rst">https://hg.python.org/sandbox/fatpython/file/tip/ASTOPTIMIZER.rst</a><br><div>
<br>
I implemented the most basic optimization to test my code: replace calls to builtin functions (with constant arguments) with the result. For example, len("abc") is replaced with 3. I reached the second milestone: it's now possible to run the full Python test suite with these optimizations enabled. It confirms that the optimizations don't break the Python semantic.<br><br></div><div>Example:<br>---<br>>>> def func():<br>...     return len("abc")<br>... <br>>>> import dis<br>>>> dis.dis(func)<br>  2           0 LOAD_GLOBAL              0 (len)<br>              3 LOAD_CONST               1 ('abc')<br>              6 CALL_FUNCTION            1 (1 positional, 0 keyword pair)<br>              9 RETURN_VALUE<br><br>>>> len(func.get_specialized())<br>1<br>>>> specialized=func.get_specialized()[0]<br>>>> dis.dis(specialized['code'])<br>  2           0 LOAD_CONST               1 (3)<br>              3 RETURN_VALUE<br>>>> len(specialized['guards'])<br>2<br><br>>>> func()<br>3<br><br>>>> len=lambda obj: "mock"<br>>>> func()<br>'mock'<br>>>> func.get_specialized()<br>[]<br>---<br><br></div><div>The function func() has specialized bytecode which returns directly 3 instead of calling len("abc"). The specialized bytecode has two guards dictionary keys: builtins.__dict__['len'] and globals()['len']. If one of these keys is modified, the specialized bytecode is simply removed (when the function is called) and the original bytecode is executed.<br></div><div><br><br>You cannot expect any speedup at this milestone, it's just to validate 
the implementation. You can only get speedup if you implement *manually*
 optimizations. See for example posixpath.isabs() which inlines manually
 the call to the _get_sep() function. More optimizations will be implemented in the third milestone. I don't know yet if I will be able to implement constant folding, function inlining and/or moving invariants out of loops.<br><br>
<br>Download, compile and test FAT Python with:<br>
<br>    hg clone <a href="http://hg.python.org/sandbox/fatpython">http://hg.python.org/sandbox/fatpython</a><br>
    ./configure && make && ./python -m test test_astoptimizer test_fat<br><br><br></div><div>Currently, only 24 functions are specialized in the standard library. Calling a builtin function with constant arguments in not common (it was expected, it's only the first step for my optimizer). But 161 functions are specialized in tests.<br><br></div><div>
<br></div><div>To be honest, I had to modify some tests to make them pass in FAT mode. But most changes are related to the .pyc filename, or to the exact size in bytes of dictionary objects.<br><br></div><div>FAT Python is still experimental. Currently, the main bug is that the AST optimizer can optimize a call to a function which is not the expected builtin function. I already started to implement code to understand namespaces (detect global and local variables), but it's not enough yet to detect when a builtin is overriden. See TODO.rst for known bugs and limitations.<br></div><div><br>
Victor<br>
</div></div>