[Python-Dev] Idea: Dictionary references

Victor Stinner victor.stinner at gmail.com
Thu Dec 17 18:32:43 EST 2015


2015-12-17 23:17 GMT+01:00 Andrew Barnert via Python-Dev
<python-dev at python.org>:
> Builtins do two dict lookups.
>
> So, the only thing you can optimize there is builtins. But maybe that's worth it.

FYI I implemented an optimization in FAT Python to avoid lookups for
builtin functions, builtin functions are copied to code constants at
runtime:
https://faster-cpython.readthedocs.org/fat_python.html#copy-builtin-functions-to-constants

It's nothing new, it's the generalization of common hacks, like 'def
func(len=len): return len(3)'.

The optimization is restricted to loading builtin symbols which are
not expected to be modified ;-)

(Right now the optimization is disabled by default, because the
optimizer is unable to detect when builtins are modified in the
current function, and so it breaks the Python semantic.)

> Class attributes (including normal methods, @property, etc.) do two or more dict lookups--first the instance, then the class, then each class on the class's MRO.

Note: Types have an efficient cache for name lookups ;-) Thanks for
this cache, it's no more an issue to have a deep hierarchy of classes.

Victor


More information about the Python-Dev mailing list