[Python-Dev] Making builtins more efficient

Phillip J. Eby pje at telecommunity.com
Thu Mar 9 16:27:18 CET 2006

At 08:50 AM 3/9/2006 -0600, Steven Elliott wrote:
>I believe that currently "mod.str = my_str" alters the module's global
>hash table (f->f_globals in the code).  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.

It's not the opcode that would change, it's the C function referenced by 
the module type's tp_setattro function slot.  This has already been 
attempted before, in order to implement a warning for this behavior.  You 
might want to research that, because the patch ended up being backed out 
for some reason.  I think it ended up being too strict of a check to be 
accepted for Python 2.4.

If some version of it could come back, however, it's possible we could use 
this in Python 2.5 to allow -O compilation to assume that unshadowed 
builtins are static, making it potentially possible to convert some of them 
to specialized bytecodes, or perhaps a "BUILTIN_OP n" bytecode, where 'n' 
is a combination of an operation selector and the number of arguments to be 
taken from the stack.

The determination of "unshadowed" would have to be conservative, in that 
'from foo import *' might shadow a builtin, so using 'import *' would 
disable optimization of all builtins.  However, if that were not present, 
and there's no statically detectable assignment shadowing a particular 
builtin, that builtin could be optimized.  (Assuming -O, of course.)

More information about the Python-Dev mailing list