__slots__

Patrick Maupin pmaupin at speakeasy.net
Mon Dec 8 22:10:07 EST 2003


Mirko Zeibig wrote:

> So while there is a big memory saving in using __slots__, the time savings I
> observed are relatively small. Test system is a Pentium Celeron 500MHz with 256
> MB running the Python 2.2 on Linux. Of course never trust measurements you
> didn't manipulate yourself ;-).

Always excellent advice, which I usually attempt to follow.  I think
I tried to make it fairly clear in the post you were responding to that
__slots__ would not always give great results.  However, some empirical
evidence I have (aka measurements I manipulated myself :) shows that in
_some_ cases the time savings can be substantial.  In the cases I have
seen, the time savings actually follow the memory savings.  In other
words, if __slots__ can make the difference between paging or not, or
even, possibly, lots of RAM cache misses or not, then it could be
worthwhile.  (As your detailed post makes clear, saving 70% of the RAM
by using __slots__ on simple objects is not out of the question, although
I do find it somewhat surprising that on your small objects the __slots__
requirement was even smaller than a tuple!)

Also, if I am not misremembering (it was quite a few months back),
I think the speed benefit was more pronounced in 2.3 than in 2.2,
so maybe there are some additional interpreter optimizations there.

FWIW, my use case for __slots__ involved creating at least tens of
millions of objects, and I was not exaggerating when I wrote that
I was on the way to writing a C extension (in Pyrex).

Regards,
Pat




More information about the Python-list mailing list