[Python-Dev] CALL_ATTR, A Method Proposal

Thomas Heller theller@python.net
14 Feb 2003 13:56:16 +0100


Glyph Lefkowitz <glyph@twistedmatrix.com> writes:

> Method calls appear are a full 20% slower (simple benchmark included) than
> function calls, and Python function calls are already pretty slow.  By my
> understanding, one of the reasons for the difference is that if you have a
> method call like this:
> 
>     a = A()
>     a.b()
> 
> what's really happening is something along the lines of:
> 
>     temp = new.instancemethod(getattr(a.__class__, "b"), a, A)
>     temp()
>     free(temp)
> 
> This causes an unnecessary memory allocation: since the instancemethod object
> is immediately being created, then called, then garbage collected.  Looking at
> the output of dis.dis, I can see there are also 3 bytecodes being evaluated
> rather than 1.

Can this allocation be avoided? Ahh, by the 'atomic' implementation of
the single CALL_ATTR opcode, using a static allocated instancemethod
instead of a new one?  Is this what you have in mind?

Thomas