[pypy-svn] r80025 - in pypy/trunk/pypy/objspace/std: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Mon Dec 13 10:47:00 CET 2010


Author: cfbolz
Date: Mon Dec 13 10:46:59 2010
New Revision: 80025

Modified:
   pypy/trunk/pypy/objspace/std/dictmultiobject.py
   pypy/trunk/pypy/objspace/std/test/test_dictmultiobject.py
Log:
implement an EmptyDictImplementation, that most dictionaries start out as. This
makes dictionaries that are just allocated and never filled much more efficient
(which apparently happens more often than you would think, judging by our memory
studies).


Modified: pypy/trunk/pypy/objspace/std/dictmultiobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/dictmultiobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/dictmultiobject.py	Mon Dec 13 10:46:59 2010
@@ -55,9 +55,8 @@
         else:
             if w_type is None:
                 w_type = space.w_dict
-            w_self = space.allocate_instance(W_DictMultiObject, w_type)
-            W_DictMultiObject.__init__(w_self, space)
-            w_self.initialize_as_rdict()
+            w_self = space.allocate_instance(EmptyDictImplementation, w_type)
+            EmptyDictImplementation.__init__(w_self, space)
             return w_self
 
     def __init__(self, space):
@@ -107,7 +106,7 @@
     def impl_setitem_str(self, key, w_value):
         raise NotImplementedError("abstract base class")
 
-    def impl_setitem(self,  w_key, w_value):
+    def impl_setitem(self, w_key, w_value):
         raise NotImplementedError("abstract base class")
 
     def impl_delitem(self, w_key):
@@ -408,6 +407,45 @@
         return self.shadowed[i]
 
 
+class EmptyDictImplementation(W_DictMultiObject):
+    def __init__(self, space):
+        self.space = space
+
+    def impl_setitem(self, w_key, w_value):
+        self._as_rdict().impl_fallback_setitem(w_key, w_value)
+
+    def impl_setitem_str(self, key, w_value):
+        self._as_rdict().impl_fallback_setitem_str(key, w_value)
+
+    def impl_delitem(self, w_key):
+        raise KeyError
+
+    def impl_length(self):
+        return 0
+
+    def impl_getitem_str(self, key):
+        return None
+
+    def impl_getitem(self, w_key):
+        # in case the key is unhashable, try to hash it
+        self.space.hash(w_key)
+        # return None anyway
+        return None
+
+    def impl_iter(self):
+        # XXX I guess it's not important to be fast in this case?
+        return self._as_rdict().impl_fallback_iter()
+
+    def impl_clear(self):
+        self.r_dict_content = None
+
+    def _as_rdict(self):
+        r_dict_content = self.initialize_as_rdict()
+        return self
+
+    def _clear_fields(self):
+        pass
+
 class RDictIteratorImplementation(IteratorImplementation):
     def __init__(self, space, dictimplementation):
         IteratorImplementation.__init__(self, space, dictimplementation)

Modified: pypy/trunk/pypy/objspace/std/test/test_dictmultiobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/test/test_dictmultiobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/test/test_dictmultiobject.py	Mon Dec 13 10:46:59 2010
@@ -16,6 +16,7 @@
         space = self.space
         d = self.space.newdict()
         assert not self.space.is_true(d)
+        assert d.r_dict_content is None
 
     def test_nonempty(self):
         space = self.space



More information about the Pypy-commit mailing list