[pypy-svn] r76822 - pypy/trunk/pypy/rpython/lltypesystem

arigo at codespeak.net arigo at codespeak.net
Wed Sep 1 19:34:37 CEST 2010


Author: arigo
Date: Wed Sep  1 19:34:36 2010
New Revision: 76822

Modified:
   pypy/trunk/pypy/rpython/lltypesystem/lltype.py
Log:
Add two caches when making new types, for Ptr() and for
FixedSizeArray(1).  These have been found to be consuming
really a lot of memory.


Modified: pypy/trunk/pypy/rpython/lltypesystem/lltype.py
==============================================================================
--- pypy/trunk/pypy/rpython/lltypesystem/lltype.py	(original)
+++ pypy/trunk/pypy/rpython/lltypesystem/lltype.py	Wed Sep  1 19:34:36 2010
@@ -401,6 +401,19 @@
     # behaves more or less like a Struct with fields item0, item1, ...
     # but also supports __getitem__(), __setitem__(), __len__().
 
+    _cache = weakref.WeakKeyDictionary()  # cache the length-1 FixedSizeArrays
+    def __new__(cls, OF, length, **kwds):
+        if length == 1 and not kwds:
+            try:
+                obj = FixedSizeArray._cache[OF]
+            except KeyError:
+                obj = FixedSizeArray._cache[OF] = Struct.__new__(cls)
+            except TypeError:
+                obj = Struct.__new__(cls)
+        else:
+            obj = Struct.__new__(cls)
+        return obj
+
     def __init__(self, OF, length, **kwds):
         fields = [('item%d' % i, OF) for i in range(length)]
         super(FixedSizeArray, self).__init__('array%d' % length, *fields,
@@ -610,6 +623,16 @@
 class Ptr(LowLevelType):
     __name__ = property(lambda self: '%sPtr' % self.TO.__name__)
 
+    _cache = weakref.WeakKeyDictionary()  # cache the Ptrs
+    def __new__(cls, TO):
+        try:
+            obj = Ptr._cache[TO]
+        except KeyError:
+            obj = Ptr._cache[TO] = LowLevelType.__new__(cls)
+        except TypeError:
+            obj = LowLevelType.__new__(cls)
+        return obj
+
     def __init__(self, TO):
         if not isinstance(TO, ContainerType):
             raise TypeError, ("can only point to a Container type, "



More information about the Pypy-commit mailing list