[Twisted-Python] Re: __slots__ help (Re: Memory size of Deferreds)

Martin Geisler mg at daimi.au.dk
Tue May 20 14:01:41 EDT 2008


Marcin Kasperski <Marcin.Kasperski at softax.com.pl> writes:

> It is not that difficult to see what is inside the deferred (spawn
> some debugger and see):
>
> [...]

Yeah, I have already looked at the code for the Deferred class -- I like
it, it is delightfully small! :-)

> (standard Deferred)
>
> $ python testdef.py
> UID        PID  PPID  C    SZ   RSS PSR STIME TTY      STAT   TIME CMD
> marcink  23109 20536 92 15180 56992   1 15:40 pts/18   S+     0:00 python testdef.py
> UID        PID  PPID  C    SZ   RSS PSR STIME TTY      STAT   TIME CMD
> marcink  23109 20536 99 28374 109000  1 15:40 pts/18   S+     0:02 python testdef.py
> UID        PID  PPID  C    SZ   RSS PSR STIME TTY      STAT   TIME CMD
> marcink  23109 20536 99 41568 161020  1 15:40 pts/18   S+     0:05 python testdef.py
>
> (Deferred with __slots__ added)
>
> $ python testdef.py
> UID        PID  PPID  C    SZ   RSS PSR STIME TTY      STAT   TIME CMD
> marcink  23127 20536  0  7834 28068   1 15:41 pts/18   S+     0:00 python testdef.py
> UID        PID  PPID  C    SZ   RSS PSR STIME TTY      STAT   TIME CMD
> marcink  23127 20536 99 13683 51160   0 15:41 pts/18   S+     0:01 python testdef.py
> UID        PID  PPID  C    SZ   RSS PSR STIME TTY      STAT   TIME CMD
> marcink  23127 20536 99 19532 74256   1 15:41 pts/18   S+     0:02 python testdef.py
>
> As one can see, the memory usage is ~ halved.

Cool -- that is a substantial saving! I did an experiment where I tested
these classes:

  >>> class X(object):
  ...   __slots__ = ('foo',) # No dict, space for one attribute only.
  ...   def __init__(self, foo):
  ...     self.foo = foo
  ... 
  >>> class Y(object):
  ...   def __init__(self, foo):
  ...     self.foo = foo

and found that X objects take up about 40 bytes in memory whereas Y
objects take up 180 bytes in memory. This mail:

  http://mail.python.org/pipermail/python-list/2002-March/135223.html

explains that a dictionary takes up 144 bytes in memory and that fits
very nicely with the savings I saw with the X and Y classes above.

-- 
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multi-Party Computation) to Python. See: http://viff.dk/.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 188 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20080520/c8ae1b72/attachment.pgp 


More information about the Twisted-Python mailing list