[Python-Dev] Making builtins more efficient

Steven Elliott selliott4 at austin.rr.com
Wed Mar 15 06:07:27 CET 2006

On Thu, 2006-03-09 at 08:51 -0800, Raymond Hettinger wrote:
> [Steven Elliott]
> > 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.
> If someone really cared about the double lookup, they could flatten a level by 
> starting their modules with:
>    from __builtin__ import *
> However, we don't see people writing this kind of code.  That could mean that 
> the double lookup hasn't been a big concern.

It could mean that.  I think what you are suggesting is sufficiently
cleaver that the average Python coder may not have thought of it.

In any case, many people are willing to do "while 1" instead of "while
True" to avoid the double lookup.  And the "from __builtin__ import *"
additionally imposes a startup cost and memory cost (at least a word per
builtin, I would guess).

> > Why not have a means of referencing the default builtins with some sort
> > of index the way the LOAD_FAST op code currently works?
> FWIW, there was a PEP proposing a roughly similar idea, but the PEP encountered 
> a great deal of resistance:
>   http://www.python.org/doc/peps/pep-0329/
> When it comes time to write your PEP, it would helpful to highlight how it 
> differs from PEP 329 (i.e. implemented through the compiler rather than as a 
> bytecode hack, etc.).

I'm flattered that you think it might be worthy of a PEP.  I'll look
into doing that.

> > 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".
> That is going to be difficult as long as it is legal to write:
>     True = 0

"LOAD_BUILTIN" (or whatever we want to call it) should be as fast as
"LOAD_FAST" (locals) or "LOAD_CONST" in that they each index into an
array where the index is the argument to the opcode.  

I'll look into writing a PEP.

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

More information about the Python-Dev mailing list