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

Random832 random832 at fastmail.com
Wed Oct 21 18:52:01 CEST 2015


Victor Stinner <victor.stinner at gmail.com>
writes:

> Hi,
>
> I would like to share with you an idea to try to optimize CPython.
>
> My previous attempts to optimize CPython failed because they changed the
> language semantic. For example, it's not possible to replace len('abc')
> with 3 because it is technically possible to override the builtin len()
> function.

I think this is solving the wrong problem...

What about adding an explicit way to "bake in" the values of globals?

freeze_globals(func, values)

values is a dict with the names of globals and their values, so, you
could for example:

def foo():
    return len('abc')

foo = freeze_globals(foo, {'len': len})
# or
foo = freeze_globals(foo, __builtins__.__dict__)

It would replace any LOAD_GLOBAL for an item that is in the given
dictionary (maybe excluding anything that is also stored in the same
function) with a LOAD_CONST with a reference to the value from the
dictionary. As a second pass, now that the bytecode looks like this:

LOAD_CONST ('abc')
LOAD_CONST (<built-in function len>)
CALL_FUNCTION 1

This is no longer dependent on anything that can be overridden, and can
safely be replaced (given the optimizer knows len is a pure function)
with the constant 3. This second pass could also take care of e.g. empty
set and frozenset constructors, etc.

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.



More information about the Python-ideas mailing list