3 ms·
> there is no function inlining in CPython. Couldn't this be done with a decorator that on the first call to the decorated function looks up the variable bindi
by ptx 11y ago
> there is no function inlining in CPython.
Couldn't this be done with a decorator that on the first call to the decorated function looks up the variable bindings, extracts the bytecode of called functions and then merges it into its own (and renames variables etc.)?
I'll leave the implementation as an exercise for the reader. :)
- barosl 11y agoThis is certainly an interesting idea. A quick Google search gave me this toy example: [1][2] Of course making it work for all cases would be very hard due to the excessive dynamism of Python, but for simple arithmetic operations it could be made to work fairly well. [1] http://tomforb.es/automatically-inline-python-function-calls http://tomforb.es/automatically-inline-python-function-calls [2] https://github.com/orf/inliner https://github.com/orf/inliner
- e12e 11y agoFrom some searching, this apparently is easier said than done[1] (read: there doesn't appear to be a way to guarantee that the decorator wouldn't break due to various meta-programming etc). Relevant links: The RPython sub-set used to implement pypy (and other languages on the rpython run-time) has a decorator for @always_inline, and does automagic inlining: https://mail.python.org/pipermail/pypy-commit/2014-November/086702.html https://mail.python.org/pipermail/pypy-commit/2014-November/... http://rpython.readthedocs.org/en/latest/translation.html#function-inlining http://rpython.readthedocs.org/en/latest/translation.html#fu... I'm not entirely clear on what is being governed by the pypy interpreter inline_trehshold -- if it's for both python code, or just for the interpreter: http://pypy.readthedocs.org/en/latest/config/translation.backendopt.inline_threshold.html http://pypy.readthedocs.org/en/latest/config/translation.bac... http://boxbase.org/entries/2015/apr/20/rpython-jit-optimizations/ http://boxbase.org/entries/2015/apr/20/rpython-jit-optimizat... There's a project to do AST level optimizations at (bytecode) compile time: https://pypi.python.org/pypi/astoptimizer https://pypi.python.org/pypi/astoptimizer There's been talk of using a decorator to allow for function inlining: [1] http://bugs.python.org/issue10399 http://bugs.python.org/issue10399