Speed of attribute lookup (was Re: advice on programming style: is multiple inheritance bad?)

Greg Chapman glc at well.com
Wed Feb 4 17:44:39 CET 2004

On 1 Feb 2004 22:42:56 -0500, aahz at pythoncraft.com (Aahz) wrote:

>The problem with attribute lookup in new-style classes is that any
>instance attribute could be a property.  So if Python finds an attribute
>on the instance, it needs to traverse the MRO to check if there's a
>property.  If there isn't one, that's an expensive waste.
>Guido, Ping, and I worked on a patch to fix that at last year's PyCon
>sprint, but one common case slowed down enough that Guido vetoed the
>patch.  Anyone who wants to bring fresh eyeballs would be welcome.
>(Don't remember the precise specifics off-hand, but I'm willing to work
>with someone who wants to try.)

I assume the work you did last year is the cache-attr-branch in the CVS?  Is
there some documentation somewhere that more fully describes the reasons for
your approach (and possibly rejected alternatives)?

I wonder if you considered turning off the cache for types with no instance dict
(tp_dictoffset == 0)?  The slowdown in access to attributes defined in the first
class in the mro is probably most critical for built-in types (without instance
dicts), which access both methods and data using class attributes.  For types
implemented in Python, I for one would be willing to trade some slow down in
accessing class attributes (generally a prelude to making a Python function
call, which is going to be relatively slow anyway), for a speed up in accessing
instance attributes.

Greg Chapman

More information about the Python-list mailing list