[Python-Dev] pystone(object)

Guido van Rossum guido@python.org
Fri, 16 Aug 2002 15:55:52 -0400

> Anyone interested in making pystone use a new-style class?  I just
> tried it and it slowed pystone down by 12%.  Using __slots__ bought
> back 5%.

Yeah, I've noticed the same (though I think I got back more than 5%
with slots).

> On the one hand, we end up comparing the new-style class
> implementation of one Python with the classic class version of older
> Pythons.  On the other hand, we seems to think that new-style classes
> are preferred.  I think we ought to measure them.

It'll be hard to close the gap completely.  For new-style classes,
instance getattr and setattr operations requires at least two dict
lookups ops: it must look in the instance dict as well as in the class
dict (and in the base classes, in MRO order).  This is so that
properties (and other descriptors that support the __set__ protocol)
can override instance variables: setattr can't just store into the
instance dict, it has to check for a property first; and similar for
getattr (it shouldn't trust the instance dict unless there's nothing
in the class).

Slots can get you back most of this, but not all.  Dict lookup is
already extremely tight code, and when I profiled this, most of the
time was spent there -- twice as many lookup calls using new-style
classes than for classic classes.

--Guido van Rossum (home page: http://www.python.org/~guido/)