[Patches] [ python-Patches-502415 ] optimize attribute lookups

noreply@sourceforge.net noreply@sourceforge.net
Thu, 17 Jan 2002 12:33:44 -0800


Patches item #502415, was opened at 2002-01-11 10:07
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=502415&group_id=5470

Category: Core (C code)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Zooko Ozoko (zooko)
Assigned to: Nobody/Anonymous (nobody)
Summary: optimize attribute lookups

Initial Comment:
This patch optimizes the string comparisons in
class_getattr(), class_setattr(), instance_getattr1(),
and instance_setattr().

I pulled out the relevant section of class_setattr()
and measured its performance, yielding the following
results:

 * in the case that the argument does *not* begin with
"__", then the new version is 1.03 times as fast as the
old.  (This is a mystery to me, as the path through the
code looks the same, in C.  I examined the assembly
that GCC v3.0.3 generated in -O3 mode, and it is true
that the assembly for the new version is
smaller/faster, although I don't really understand why.)

 * in the case that the argument is a string of random
length between 1 and 19 inclusive, and it begins with
"__" and ends with "X_" (where X is a random alphabetic
character), then the new version 1.12 times as fast as
the old.

 * in the case that the argument is a string of random
length between 1 and 19 inclusive, and it begins with
"__" and does *not* end with "_", then the new version
1.16 times as fast as the old.

 * in the case that the argument is (randomly) one of
the six special names, then the new version is 2.7
times as fast as the old.

 * in the case that the argument is a string of random
length between 1 and 19 inclusive, and it begins with
"__" and ends with "__" (but is not one of the six
special names), then the new version is 3.7 times as
fast as the old.



----------------------------------------------------------------------

>Comment By: Zooko Ozoko (zooko)
Date: 2002-01-17 12:33

Message:
Logged In: YES 
user_id=52562

Yeah, the optimized version is less readable that the original.

I'll try to come up with a benchmark application.  Any
ideas?  Maybe some unit tests from Zope that use attribute
lookups heavily?

My guess is that the actual results in an application will
be "marginal", like maybe between 0.5% to 3% improvement.



----------------------------------------------------------------------

Comment By: Jeremy Hylton (jhylton)
Date: 2002-01-17 10:29

Message:
Logged In: YES 
user_id=31392

This seems to add a lot of complexity for a few special
cases.  How important are these particular attributes?  Do
you have any benchmark applications that show real
improvement?  It seems like microbenchmarks overstate the
benefit, since we don't know how often these attributes are
looked up by most applications.

It would also be interesting to see how much of the benefit
for non __ names is the result of the PyString_AS_STRING()
macro.  Maybe that's all the change we really need :-).


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=502415&group_id=5470