[pypy-svn] r54589 - in pypy/branch/gc-tweak/pypy/rpython/memory: . gc test

arigo at codespeak.net arigo at codespeak.net
Fri May 9 14:01:05 CEST 2008


Author: arigo
Date: Fri May  9 14:01:04 2008
New Revision: 54589

Modified:
   pypy/branch/gc-tweak/pypy/rpython/memory/gc/generation.py
   pypy/branch/gc-tweak/pypy/rpython/memory/lldict.py
   pypy/branch/gc-tweak/pypy/rpython/memory/support.py
   pypy/branch/gc-tweak/pypy/rpython/memory/test/test_lldict.py
Log:
A further small tweak.


Modified: pypy/branch/gc-tweak/pypy/rpython/memory/gc/generation.py
==============================================================================
--- pypy/branch/gc-tweak/pypy/rpython/memory/gc/generation.py	(original)
+++ pypy/branch/gc-tweak/pypy/rpython/memory/gc/generation.py	Fri May  9 14:01:04 2008
@@ -468,13 +468,16 @@
             return SemiSpaceGC._compute_id(self, obj)
 
     def update_young_objects_with_id(self):
-        self.young_objects_with_id.foreach(self._update_object_id,
-                                           self.objects_with_id)
-        self.young_objects_with_id.clear()
+        # 'foreach_clear' iterates over the items in the dict and clears
+        # it in the same pass.  It does not shrink the dictionary, which
+        # is appropriate for our use case: the dict will tend to be large
+        # enough for the maximum number of objects with id that exist in
+        # the nursery, and stay at that size for the rest of the execution.
+        self.young_objects_with_id.foreach_clear(self._update_object_id,
+                                                 self.objects_with_id)
 
     def ids_grow_older(self):
-        self.young_objects_with_id.foreach(self._id_grow_older, None)
-        self.young_objects_with_id.clear()
+        self.young_objects_with_id.foreach_clear(self._id_grow_older, None)
 
     def _id_grow_older(self, obj, id, ignored):
         self.objects_with_id.setitem(obj, id)

Modified: pypy/branch/gc-tweak/pypy/rpython/memory/lldict.py
==============================================================================
--- pypy/branch/gc-tweak/pypy/rpython/memory/lldict.py	(original)
+++ pypy/branch/gc-tweak/pypy/rpython/memory/lldict.py	Fri May  9 14:01:04 2008
@@ -69,6 +69,19 @@
         i -= 1
 dict_foreach._annspecialcase_ = 'specialize:arg(1)'
 
+def dict_foreach_clear(d, callback, arg):
+    entries = d.entries
+    i = len(entries) - 1
+    while i >= 0:
+        if dict_entry_valid(entries, i):
+            key = entries[i].key
+            entries[i].key = llmemory.NULL
+            callback(key, entries[i].value, arg)
+        i -= 1
+    d.num_items = 0
+    d.num_pristine_entries = len(entries)
+dict_foreach_clear._annspecialcase_ = 'specialize:arg(1)'
+
 ENTRY = lltype.Struct('ENTRY', ('key', llmemory.Address),
                                ('value', llmemory.Address))
 ENTRIES = lltype.Array(ENTRY,
@@ -91,8 +104,8 @@
                          'get': dict_get,
                          'add': dict_add,
                          'insertclean': dict_insertclean,
-                         'clear': rdict.ll_clear,
                          'foreach': dict_foreach,
+                         'foreach_clear': dict_foreach_clear,
                          'keyhash': dict_keyhash,
                          'keyeq': None,
                      })

Modified: pypy/branch/gc-tweak/pypy/rpython/memory/support.py
==============================================================================
--- pypy/branch/gc-tweak/pypy/rpython/memory/support.py	(original)
+++ pypy/branch/gc-tweak/pypy/rpython/memory/support.py	Fri May  9 14:01:04 2008
@@ -257,7 +257,8 @@
     def add(self, keyaddr):
         self.setitem(keyaddr, llmemory.NULL)
 
-    def clear(self):
+    def foreach_clear(self, callback, arg):
+        self.foreach(callback, arg)
         self.data.clear()
 
     def foreach(self, callback, arg):

Modified: pypy/branch/gc-tweak/pypy/rpython/memory/test/test_lldict.py
==============================================================================
--- pypy/branch/gc-tweak/pypy/rpython/memory/test/test_lldict.py	(original)
+++ pypy/branch/gc-tweak/pypy/rpython/memory/test/test_lldict.py	Fri May  9 14:01:04 2008
@@ -60,12 +60,18 @@
         d2.delete()
         assert lldict.alloc_count == 0
 
-    def test_clear(self):
+    def test_foreach_clear(self):
         d = lldict.newdict()
         d.setitem(intaddr(41), intaddr(42))
-        d.clear()
+        result = []
+        d.foreach_clear(lambda key, value, arg: result.append((key,value,arg)),
+                        "hello world")
         assert d.length() == 0
         assert not d.contains(intaddr(41))
+        assert len(result) == 1
+        assert result[0][0].intval == 41
+        assert result[0][1].intval == 42
+        assert result[0][2] == "hello world"
         d.delete()
         assert lldict.alloc_count == 0
 



More information about the Pypy-commit mailing list