[Python-Dev] Release of astoptimizer 0.3

Victor Stinner victor.stinner at gmail.com
Wed Sep 12 02:27:07 CEST 2012

> i am a longtime Reader of this list and this is the first time i a dare to
> speak up.
> Apology in advance for any noise, silly comments and not posting to
> python-ideas ;).


> Well how about implementing guards like in pypy?

Guards would allow to generate specialized functions without a JIT.

Dummy example:

def func(arg):
  arg *= 1
  arg += 0
  return arg * 16

Can be optimized to:

def func(arg):
  if type(arg) is int:
    # fast path
    return arg << 4
    # slow path
    arg *= 1
    arg += 0
    return arg * 16

I prefer a check before executing the code, rather than an exception
and reexecute the same code twice.

Specializing all functions would waste a lot of memory, so it should
only be applied on some very specific cases... It can also be slower
if the guard check is slower than the function body! But I bet that
guards would help to enable more aggressive optimizations, or at least
make some optimizations safe.

Dave Malcolm wrote a patch modifying eval.c to support specialized
functions. See the

I don't know yet what is the best approach for CPython.


For the specific case of builtin functions and types, I made two
changes in Python 3.3:

 * the type of the builtins mapping can now be any mapping (Python <=
3.2 requires the dict type, dict subtypes are disallowed)
 * "new" types.MappingProxyType type to create a read-only proxy for a
mapping (see also the rejected PEP 416)

We may combine these two changes to use a read-only mapping for
builtins. It would at least help to ensure that an application does
not monkeypatch builtin functions/types.


More information about the Python-Dev mailing list