[Psycopg] decrease DictRow memory usage
Oswaldo Hernández
listas at soft-com.es
Tue May 5 15:55:10 CEST 2009
James Henstridge escribió:
> 2009/5/5 Oswaldo Hernández <listas at soft-com.es>:
>> Federico Di Gregorio escribió:
>>> Il giorno lun, 04/05/2009 alle 16.22 +0300, Marko Kreen ha scritto:
>>>> Use "__slots__ = ('_index',)" to reserve single attribute slot for
>>>> _index.
>>>> Otherwise a per-row dict is created to store object attributes...
>>>>
>>>> The only danger of doing it is that if somebody uses plain DictRow and
>>>> adds new attrubutes into it, it will get error:
>>>>
>>>> row.foo = x
>>>>
>>>> I'm really dubious if anybody does this. NB, this does not apply if
>>>> somebody has subclassed DictRow, then the parent class' __slots__
>>>> is ignored.
>>> I agree. I someone uses custom attributes on the DictRow let me know or
>>> I'll apply the patch. ;)
>>>
>> Hi,
>>
>> I use a custom DictRow class derived from extras.DictRow. I will do this
>> afternoon some tests to overload '__slots__'.
>>
>> I will put here the results tomorrow.
>
> If you're subclassing DictRow then this change shouldn't have any
> effect on you. Your subclass will have an instance dictionary though,
> so you won't see the memory savings.
>
> If you want to see the memory savings though, you'll need to define
> __slots__ for your subclass. If you aren't adding any attributes,
> setting it to an empty tuple will do the trick.
>
Yes i add one attribute in my custom class.
I apply the patch, then on an initial test:
. If i do nothing on my subclass, python provides a __dict__ for it. Works fine.
. Adding in the subclass:
__slots__ = ('myattr',)
also works fine.
Seems no problems about using slots on main DictRow and subclasing it.
Thanks,
--
*****************************************
Oswaldo Hernández
oswaldo (@) soft-com (.) es
*****************************************
PD:
Antes de imprimir este mensaje, asegúrese de que es necesario.
El medio ambiente está en nuestra mano.
More information about the Psycopg
mailing list