[Python-ideas] Optimizing builtins
stefan_ml at behnel.de
Sun Jan 2 10:10:51 CET 2011
Benjamin Peterson, 01.01.2011 21:35:
> Guido van Rossum<guido at ...> writes:
>> The compiler has no way to notice this when a.py is being compiled.
> You could still optimize it if you insert a runtime "guard" before the range
> usage and see if its been overridden.
The problem here is that you wouldn't save the lookup. So you'd still pay a
high price to find out that the builtin has not been overridden.
There can be substantial savings for builtins that can be optimised away or
replaced by a tighter/adapted implementation. We do that a lot in Cython
where builtins are (by default) considered static unless redefined inside
of the module. An important example are generator expressions like
"any(genexpr)". If the function was known to be builtin at compile time,
CPython could generate much simpler byte code for these, dropping the need
for a generator and its closure.
But as long as you have to check for an override at each call, you end up
with the duplicated code (optimised and fall-back version) and an increased
entry overhead that may well kill the savings.
More information about the Python-ideas