[Python-Dev] LOAD_SELF and SELF_ATTR opcodes

Phillip J. Eby pje at telecommunity.com
Sat Oct 15 01:33:44 CEST 2005

At 12:33 AM 10/15/2005 +0200, Martin v. Löwis wrote:
>Phillip J. Eby wrote:
> > Anyway, my main question is, do these sound like worthwhile
> > optimizations?
>In the past, I think the analysis was always "no". It adds
>an opcode, so increases the size of the switch, causing
>more pressure on the cache, with an overall questionable

Hm.  I'd have thought 5% pystone and 2% pybench is nothing to sneeze at, 
for such a minor change.  I thought Skip's peephole optimizer originally 
only produced a 5% or so speedup.

In any case, in relation to this specific kind of optimization, this is the 
only thing I found:


which is a proposal by Guido to do the same thing, but also speeding up the 
actual attribute lookup.  I didn't find any follow-up suggesting that 
anybody tried this, but perhaps it was put on hold pending the AST branch?  :)

>As for measuring the effect of the change: how often
>does that pattern occur in the standard library?
>(compared to what total number of LOAD_ATTR)

[pje at ns src]$ grep 'self\.[A-Za-z_]' Lib/*.py | wc -l

[pje at ns src]$ grep '[a-zA-Z_][a-zA-Z_0-9]*\.[a-zA-Z_]' Lib/*.py | wc -l

So, something like 50% of lines doing an attribute access include a 'self' 
attribute access.  This very rough estimate may be thrown off by:

* Import statements (causing an error in favor of more non-self attributes)
* Functions whose first argument isn't 'self' (error in favor of non-self 
* Comments or docstrings talking about attributes or modules (could go 
either way)
* Multiple attribute accesses on the same line (could go either way)

The parrotbench code shows a similar ratio of self to non-self attribute 
usage, but nearly all of parrotbench's self-attribute usage is in b0.py, 
and not called in the innermost loop.

That also suggests that the volume of usage of 'self.' isn't the best way 
to determine the performance impact, because pystone has almost no 'self.' 
usage at all, but still got a 5% total boost.

More information about the Python-Dev mailing list