FrozenList to reduce memory usage
authorForrest Voight <forrest@forre.st>
Sun, 31 Jul 2011 17:26:52 +0000 (13:26 -0400)
committerForrest Voight <forrest@forre.st>
Sun, 31 Jul 2011 17:26:52 +0000 (13:26 -0400)
p2pool/bitcoin/data.py

index fce4ae2..a71f961 100644 (file)
@@ -1,6 +1,7 @@
 from __future__ import division
 
 import hashlib
+import itertools
 import struct
 
 from . import base58, skiplists
@@ -181,6 +182,17 @@ class ShortHashType(Type):
             raise ValueError('invalid hash value - %r' % (item,))
         return file, ('%040x' % (item,)).decode('hex')[::-1]
 
+class FrozenList(tuple):
+    def __eq__(self, other):
+        if isinstance(other, FrozenList):
+            return tuple.__eq__(self, other)
+        elif isinstance(other, list):
+            return len(self) == len(other) and all(a == b for a, b in itertools.izip(self, other))
+        else:
+            raise TypeError()
+    def __ne__(self, other):
+        return not (self == other)
+
 class ListType(Type):
     _inner_size = VarIntType()
     
@@ -193,7 +205,7 @@ class ListType(Type):
         for i in xrange(length):
             item, file = self.type.read(file)
             res.append(item)
-        return res, file
+        return FrozenList(res), file
     
     def write(self, file, item):
         file = self._inner_size.write(file, len(item))