[Python-ideas] Accelerated attr lookups

Eyal Lotem eyal.lotem at gmail.com
Thu Jun 28 23:41:58 CEST 2007

I haven't compared benchmarks, but I strongly suspect that my patch is
not as fast for "real" programs, as in real programs, globals/builtins
are almost only exclusively read, and almost never written to.
His patch accelerates global reads by as much as mine does, without
making function object creation as expensive, and his addition of
overhead to dicts is probably smaller. My patch also accelerates
writes, but as I said, that will go nearly unnoticed normally.

My patch is not the end, but a means to an end. If the purpose is only
accelerating globals/builtins access, then the patch you linked to is
simpler and better.  The purpose I aim for, however, is to later use
the same technique to also accelerate access to the dicts in the type
or even in the instance, by specializing them in function objects.
This will allow to get rid of almost all attribute lookups in dicts.
Combined with the use of __slots__ in all classes, no dict lookups
will be used at all for attributes at all, except in "getattr" calls.
Combined with an mro cache, this should put Python very close to C in
terms of attribute access speed (simple direct access).


On 6/28/07, Giovanni Bajo <rasky at develer.com> wrote:
> On 19/06/2007 17.13, Eyal Lotem wrote:
> > Hi, I have attached a patch at:
> > https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1739789&group_id=5470
> >
> > A common optimization tip for Python code is to use locals rather than
> > globals. This converts dictionary lookups of (interned) strings to
> > tuple indexing. I have created a patch that achieves this speed
> > benefit "automatically" for all globals and builtins, by adding a
> > feature to dictobjects.
> >
> > Additionally, the idea of this patch is that it puts down the
> > necessary infrastructure to also allow virtually all attribute
> > accesses to also be
> > accelerated in the same way (with some extra work, of course).
> >
> > I have already suggested this before but I got the impression that the
> > spirit of the replies was "talk is cheap, show us the
> > code/benchmarks".  So I wrote some code.
> >
> > Getting the changes to work was not easy, and required learning about
> > the nuances of dictobject's, their re-entrancy issues, etc. These
> > changes do slow down dictobjects, but it seems that this slowdown is
> > more than offset by the speed increase of builtins/globals access.
> How does it compare with this patch:
> https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1616125&group_id=5470
> ?
> --
> Giovanni Bajo
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

More information about the Python-ideas mailing list