decrease DictRow memory usage

Marko Kreen <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
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.

Attached patch does the same for RealDictRow too, although I personally
do not use it.  But it seems good idea to do it there too.

On older python versions that do not support __slots__ it should be
taken as random class attribute, so should be no problem.

-- 
marko

_______________________________________________
Psycopg mailing list
Psycopg-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/psycopg
dictrow.diet.diff (text/x-patch, 701 B)
diff --git a/lib/extras.py b/lib/extras.py
index 4a845ba..d6337ac 100644
--- a/lib/extras.py
+++ b/lib/extras.py
@@ -107,6 +107,8 @@ class DictCursor(DictCursorBase):
 class DictRow(list):
     """A row object that allow by-colun-name access to data."""
 
+    __slots__ = ('_index',)
+
     def __init__(self, cursor):
         self._index = cursor.index
         self[:] = [None] * len(cursor.description)
@@ -191,6 +193,9 @@ class RealDictCursor(DictCursorBase):
             self._query_executed = 0           
 
 class RealDictRow(dict):
+
+    __slots__ = ('_column_mapping',)
+
     def __init__(self, cursor):
         dict.__init__(self)
         self._column_mapping = cursor.column_mapping
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.