[Python-Dev] Making builtins more efficient

Steven Elliott selliott4 at austin.rr.com
Thu Mar 9 05:48:11 CET 2006

I'm interested in how builtins could be more efficient.  I've read over
some of the PEPs having to do with making global variables more
efficient (search for "global"):
But I think the problem can be simplified by focusing strictly on

One of my assumptions is that only a small fractions of modules override
the default builtins with something like:
    import mybuiltins
    __builtins__ = mybuiltins

As you probably know each access of a builtin requires two hash table
lookups.  First, the builtin is not found in the list of globals.  It is
then found in the list of builtins.

Why not have a means of referencing the default builtins with some sort
of index the way the LOAD_FAST op code currently works?  In other words,
by default each module gets the default set of builtins indexed (where
the index indexes into an array) in a certain order.  The version stored
in the pyc file would be bumped  each time the set of default builtins
is changed.

I don't have very strong feelings whether things like True = (1 == 1)
would be a syntax error, but assigning to a builtin could just do the
equivalent of STORE_FAST.  I also don't have very strong feelings about
whether the array of default builtins would be shared between modules.
To simulate the current behavior where attempting to assign to builtin
actually alters that module's global hashtable a separate array of
builtins could be used for each module.

As to assigning to __builtins__ (like I mentioned at the beginning of
this post) perhaps it could assign to the builtin array for those items
that have a name that matches a default builtin (such as "True" or
"len").  Those items that don't match a default builtin would just
create global variables.

Perhaps what I'm suggesting isn't feasible for reasons that have already
been discussed.  But it seems like it should be possible to make "while
True" as efficient as "while 1".

|          Steven Elliott          |      selliott4 at austin.rr.com     |

More information about the Python-Dev mailing list