[Python-ideas] Optimizing global names via dict references

Victor Stinner victor.stinner at gmail.com
Sat Dec 19 10:06:09 EST 2015


Le samedi 19 décembre 2015, Franklin? Lee <leewangzhong+python at gmail.com
<javascript:_e(%7B%7D,'cvml','leewangzhong%2Bpython at gmail.com');>> a écrit :
>
> == Problem statement ==
>
> I propose a CPython interpreter optimization for global name lookups.
>

Fans of micro optimisation are probably already using various hacks like
func(len=len): ... to avoid the lookup at runtime. There is an option
rewriting bytecode to replace load_global with load_const (I don't recall
the name of the PyPI project, it's a decorator).

Serhiy also proposed to implement a new syntax to make the lookup when the
function is defined.

It would be interesting to mesure the cost of these lookups(ex: number of
nanoseconds per lookup) and have an idea on how much load_global lookups
are used in the wild (ratio on the overall number of instructions).

Since the goal is a speedup, a working proof of concept is required to show
that it works and it's faster (on macro benchmarks?). Do you feel able to
implement it?

As I already wrote, I'm not convinced that it's worth it. Your code looks
more complex, will use more memory, etc. I don't think that load_global is
common in hot code (the 10% taking 90% of the runtime). I expect effetcs on
object lifetime which could be annoying.

I implemented an optimization in FAT Python that replaces builtin lookups
with load_const. I have to enhance the code to make it safe, but it works
and it doesn't require deep changes in dict type.
http://faster-cpython.readthedocs.org/en/latest/fat_python.html#copy-builtin-functions-to-constants

In short, it only adds a single integer per dict, incremented at each
modification (create, modify, delete).

Victor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151219/ac93b0cd/attachment-0001.html>


More information about the Python-ideas mailing list