Author: Carl Friedrich Bolz <cfb...@gmx.de>
Branch: improve-heap-caching-tracing
Changeset: r47062:d79a75d32675
Date: 2011-09-04 12:00 +0200
http://bitbucket.org/pypy/pypy/changeset/d79a75d32675/

Log:    implement the same logic for getarrayitem as for getfield by reusing
        the same code

diff --git a/pypy/jit/metainterp/heapcache.py b/pypy/jit/metainterp/heapcache.py
--- a/pypy/jit/metainterp/heapcache.py
+++ b/pypy/jit/metainterp/heapcache.py
@@ -63,13 +63,16 @@
         self.heap_cache.setdefault(descr, {})[box] = fieldbox
 
     def setfield(self, box, descr, fieldbox):
+        d = self.heap_cache.get(descr, None)
+        new_d = self._do_write_with_aliasing(d, box, fieldbox)
+        self.heap_cache[descr] = new_d
+
+    def _do_write_with_aliasing(self, d, box, fieldbox):
         # slightly subtle logic here
-        d = self.heap_cache.get(descr, None)
         # a write to an arbitrary box, all other boxes can alias this one
         if not d or box not in self.new_boxes:
             # therefore we throw away the cache
-            self.heap_cache[descr] = {box: fieldbox}
-            return
+            return {box: fieldbox}
         # the object we are writing to is freshly allocated
         # only remove some boxes from the cache
         new_d = {}
@@ -80,7 +83,7 @@
             if frombox in self.new_boxes:
                 new_d[frombox] = tobox
         new_d[box] = fieldbox
-        self.heap_cache[descr] = new_d
+        return new_d
 
     def getarrayitem(self, box, descr, indexbox):
         if not isinstance(indexbox, ConstInt):
@@ -100,7 +103,8 @@
             return
         index = indexbox.getint()
         cache = self.heap_array_cache.setdefault(descr, {})
-        cache[index] = {box: valuebox}
+        indexcache = cache.get(index, None)
+        cache[index] = self._do_write_with_aliasing(indexcache, box, valuebox)
 
     def replace_box(self, oldbox, newbox):
         for descr, d in self.heap_cache.iteritems():
diff --git a/pypy/jit/metainterp/test/test_heapcache.py 
b/pypy/jit/metainterp/test/test_heapcache.py
--- a/pypy/jit/metainterp/test/test_heapcache.py
+++ b/pypy/jit/metainterp/test/test_heapcache.py
@@ -177,6 +177,36 @@
         assert h.getarrayitem(box1, descr1, index1) is None
         assert h.getarrayitem(box1, descr1, index2) is None
 
+
+    def test_heapcache_write_fields_multiple_array(self):
+        h = HeapCache()
+        h.setarrayitem(box1, descr1, index1, box2)
+        assert h.getarrayitem(box1, descr1, index1) is box2
+        h.setarrayitem(box3, descr1, index1, box4)
+        assert h.getarrayitem(box3, descr1, index1) is box4
+        assert h.getarrayitem(box1, descr1, index1) is None # box1 and box3 
can alias
+
+        h = HeapCache()
+        h.new(box1)
+        h.setarrayitem(box1, descr1, index1, box2)
+        assert h.getarrayitem(box1, descr1, index1) is box2
+        h.setarrayitem(box3, descr1, index1, box4)
+        assert h.getarrayitem(box3, descr1, index1) is box4
+        assert h.getarrayitem(box1, descr1, index1) is None # box1 and box3 
can alias
+
+        h = HeapCache()
+        h.new(box1)
+        h.new(box3)
+        h.setarrayitem(box1, descr1, index1, box2)
+        assert h.getarrayitem(box1, descr1, index1) is box2
+        h.setarrayitem(box3, descr1, index1, box4)
+        assert h.getarrayitem(box3, descr1, index1) is box4
+        assert h.getarrayitem(box1, descr1, index1) is box2 # box1 and box3 
cannot alias
+        h.setarrayitem(box1, descr1, index1, box3)
+        assert h.getarrayitem(box3, descr1, index1) is box4
+        assert h.getarrayitem(box1, descr1, index1) is box3 # box1 and box3 
cannot alias
+
+
     def test_invalidate_cache(self):
         h = HeapCache()
         h.setfield(box1, descr1, box2)
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to