[Python-ideas] Optimizing global names via dict references
M.-A. Lemburg
mal at egenix.com
Sat Dec 19 10:14:00 EST 2015
On 19.12.2015 16:06, Victor Stinner wrote:
> 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.
The effects are minimal and only show up in overall performance
if the functions in question are used a lot.
I gave a talk about such optimizations last year at PyCon UK
you might want to have a look at:
http://www.egenix.com/library/presentations/PyCon-UK-2014-When-performance-matters/
Slide 38 has the details about such lookup optimization tricks:
https://downloads.egenix.com/python/PyCon-UK-2014-When-performance-matters-Talk.pdf
> 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
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Experts (#1, Dec 19 2015)
>>> Python Projects, Coaching and Consulting ... http://www.egenix.com/
>>> Python Database Interfaces ... http://products.egenix.com/
>>> Plone/Zope Database Interfaces ... http://zope.egenix.com/
________________________________________________________________________
::: We implement business ideas - efficiently in both time and costs :::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
http://www.malemburg.com/
More information about the Python-ideas
mailing list