On Wed, Nov 18, 2009 at 11:52 PM, Dominic LoBue <[email protected]> wrote:
> On Wed, Nov 18, 2009 at 11:48 PM, Dominic LoBue <[email protected]> wrote:
>> Ian,
>>
>> I had not yet done any testing when I emailed you that patch originally.
>>
>> When I did some testing the other day in order to determine what
>> difference, if any, the __slots__ attribute made, the results were
>> surprising. My application using the __slots__ patched version of
>> urwid used 4-5 megs more ram than the unpatched version.
>>
>> I am not totally sure why this is; as I explained before, the
>> __slots__ attribute reduces an object's memory footprint by changing
>> the data structure of attributes from a dict to a tuple. My first
>> thought is that it may be from pre-allocating the memory space for the
>> _urwid_signals attribute on every object for the new way you're doing
>> signals, and that pre-allocation on every widget eliminates any
>> previous gains.
>>
>> Switching signals back to the previous implementation may solve the problem.
>>
>> As for my testing methodology, I loaded up the mail program I've been
>> developing using Urwid and loaded all conversations in my email. Doing
>> this loaded ~2650 AttrMap wrapped Text widgets into a Listbox. Once
>> all the widgets were loaded I did ps aux in another terminal and
>> looked at the RSS field.
>>
>> My program using an unmodified version of Urwid used ~143000k of ram.
>> My program using Urwid with my patch used ~148000k of ram.
>>
>> Dominic
>>
>>
>> On Wed, Nov 11, 2009 at 5:44 AM, Ian Ward <[email protected]> wrote:
>>> Dominic LoBue wrote:
>>> > As you suggested when I mentioned including __slots__, I went through
>>>> Urwid and added the __slots__ attribute where it made sense.
>>>>
>>>> Attached is a mercurial export of my changes. I've been using that
>>>> code all day today with my program and have worked out most of the
>>>> snags. The only problem I'm aware of is with the LineBox widget, I get
>>>> this error:
>>>> TypeError: Error when calling the metaclass bases
>>>> multiple bases have instance lay-out conflict
>>>>
>>>> I tried looking for a simple fix to get it working, but frankly it
>>>> made my brain hurt. >.< I finally gave in and just commented the
>>>> widget out since I wasn't using it anyway, and everything else worked.
>>>> Maybe you'll have an idea for a fix.
>>>
>>> I'm unfamiliar with __slots__, but it looks like it would break any kind
>>> of multiple inheritance. It also would cause a problem any time you
>>> want to add an attribute that wasn't declared.
>>>
>>> What are the memory footprint gains you are seeing by using __slots__?
>>> How are you measuring them?
>>>
>>> I think they may only be appropriate for some classes, like the canvas
>>> classes, and things like AttrSpec that really only ever have a few
>>> attributes, and there is no need to subclass them.
>>>
>>> Ian
>>>
>>>
>>> _______________________________________________
>>> Urwid mailing list
>>> [email protected]
>>> http://lists.excess.org/mailman/listinfo/urwid
>>>
>>>
>>
>
>
> Addendum-
> Quick example of what I'm talking about dict vs tuple:
> In [1]: import sys
>
> In [2]: a = []
>
> In [3]: s = {}
>
> In [4]: d = tuple()
>
> In [5]: sys.getsizeof(a)
> Out[5]: 72
>
> In [6]: sys.getsizeof(s)
> Out[6]: 280
>
> In [7]: sys.getsizeof(d)
> Out[7]: 56
>
Addendum v2:
Actually, it just occurred to me to test one other thing:
In [8]: class noslot(object): pass
...:
In [9]: class yesslot(object):
...: __slots__ = ('a','b')
...: pass
...:
In [10]: q = noslot()
In [11]: w = yesslot()
In [12]: sys.getsizeof(q)
Out[12]: 64
In [13]: sys.getsizeof(w)
Out[13]: 64
In [14]: class emptyslots(object):
....: __slots__ = ()
....: pass
....:
In [15]: e = emptyslots()
In [16]: sys.getsizeof(e)
Out[16]: 16
_______________________________________________
Urwid mailing list
[email protected]
http://lists.excess.org/mailman/listinfo/urwid