[Python-Dev] Making builtins more efficient

Phillip J. Eby pje at telecommunity.com
Fri Mar 10 02:29:21 CET 2006


At 12:46 PM 3/10/2006 +1300, Greg Ewing wrote:
>Steven Elliott wrote:
> > One way of handling it is to
> > alter STORE_ATTR (op code for assigning to mod.str) to always check to
> > see if the key being assigned is one of the default builtins.  If it is,
> > then the module's indexed array of builtins is assigned to.
>
>As long as you're going to all that trouble, it
>doesn't seem like it would be much harder to treat
>all global names that way, instead of just a predefined
>set. The compiler already knows all of the names that
>are used as globals in the module's code.

But knowing that an operation is a builtin allows for the possibility of 
invoking the equivalent C operation directly in the interpreter (e.g. via a 
jump table), thus letting us translate something like "len(foo)" from:

     LOAD_GLOBAL   len
     LOAD_FAST     foo
     CALL_FUNCTION 1

into something like:

     LOAD_FAST  foo
     BUILTIN_OP len, 1

And, the BUILTIN_OP could invoke a C function passing in a pointer to the 
arguments on the stack, so as to bypass tuple allocation, and the C 
function would use PySequence_Length() rather than going through the Python 
calling convention to PyObject_Call() the 'len' object.

Now, whether that'll actually speed real programs up much, I don't 
know.  But there are certainly a lot of things being cut out of the process 
using this approach, including an opcode fetch/decode, two dictionary 
lookups (one failing, one successful), and perhaps some tuplefying (only 
for C funcs w argcount>1, since those builtins don't need an argtuple IIRC).

And, even if it does speed things up a good deal, there's still a question 
of whether it should be done, when some real systems hack modules' builtins 
for testing.  However, if BUILTIN_OP were to verify at runtime that 
__builtins__ is the interpreter standard builtins (i.e., the 
restricted-mode check), then it could dynamically choose to do the slower 
operation lookup.  That would allow you to hack a module's builtins by 
giving it a fresh __builtins__ dictionary to implement that kind of 
monkeypatching.



More information about the Python-Dev mailing list