[Python-ideas] Add specialized bytecode with guards to functions

Victor Stinner victor.stinner at gmail.com
Wed Oct 21 18:59:54 CEST 2015


Hi,

2015-10-21 18:52 GMT+02:00 Random832 <random832 at fastmail.com>:
> so, you could for example:
>
> def foo():
>     return len('abc')
>
> foo = freeze_globals(foo, {'len': len})
> # or
> foo = freeze_globals(foo, __builtins__.__dict__)

You are changing the semantic of Python. In Python, you should be able
to override builtin functions.

Binding a global name to a local name is a known trick to optimize a
function, but you have to write it explicitly, and again you loose the
ability to override len.

Example:

def f(len=len):
   return len("abc")

Here len becomes a fast local variable.

> Decorators could be included to freeze only built-ins, to freeze all
> globals and built-ins, or to have a list of names to exclude. So you
> could have @freeze_builtins or @freeze_all_globals for each function
> that needs heavy optimization.

I cited "previous attempts" which failed.

What you describe is close to my "readonly" attempt which tried to
make module and type namespaces readonly:
https://hg.python.org/sandbox/readonly/file/tip/READONLY.txt

Making namespaces read-only simply doesn't work. It's very common to
modify namespaces. I added callbacks to disable optimizations when a
namespace is modified. But this design doesn't scale: you add a
complexity of O(n) when a namespace is modified, even if optimized
functions are never called.

My new design (guards) avoids this performance issue.

Victor


More information about the Python-ideas mailing list