[pypy-commit] pypy typed-cells: preserve immutable optimization better

cfbolz noreply at buildbot.pypy.org
Tue Jan 27 17:01:44 CET 2015


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: typed-cells
Changeset: r75545:f701f1ca17d3
Date: 2015-01-27 17:01 +0100
http://bitbucket.org/pypy/pypy/changeset/f701f1ca17d3/

Log:	preserve immutable optimization better

diff --git a/pypy/module/pypyjit/test_pypy_c/test_instance.py b/pypy/module/pypyjit/test_pypy_c/test_instance.py
--- a/pypy/module/pypyjit/test_pypy_c/test_instance.py
+++ b/pypy/module/pypyjit/test_pypy_c/test_instance.py
@@ -30,7 +30,7 @@
             jump(..., descr=...)
         """)
 
-    def test_load_attr(self):
+    def test_load_immutable_attr(self):
         src = '''
             class A(object):
                 pass
diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py
--- a/pypy/objspace/std/mapdict.py
+++ b/pypy/objspace/std/mapdict.py
@@ -310,7 +310,8 @@
         if type(w_value) is W_IntObject:
             if not self.can_contain_mutable_cell:
                 self.can_contain_mutable_cell = True
-            return IntMutableCell(w_value.intval)
+            if self.ever_mutated:
+                return IntMutableCell(w_value.intval)
         return w_value
 
     def _copy_attr(self, obj, new_obj):
diff --git a/pypy/objspace/std/test/test_mapdict.py b/pypy/objspace/std/test/test_mapdict.py
--- a/pypy/objspace/std/test/test_mapdict.py
+++ b/pypy/objspace/std/test/test_mapdict.py
@@ -154,10 +154,16 @@
     assert obj.map.ever_mutated == True
     assert obj.map is map1
 
-def test_mutcell():
+def test_mutcell_not_immutable():
     from pypy.objspace.std.intobject import W_IntObject
     cls = Class()
     obj = cls.instantiate()
+    # make sure the attribute counts as mutable
+    obj.setdictvalue(space, "a", W_IntObject(4))
+    obj.setdictvalue(space, "a", W_IntObject(5))
+    assert obj.map.ever_mutated
+
+    obj = cls.instantiate()
     obj.setdictvalue(space, "a", W_IntObject(5))
     # not wrapped because of the FakeSpace :-(
     assert obj.getdictvalue(space, "a") == 5
@@ -176,6 +182,18 @@
     assert mutcell2.intvalue == 7
     assert mutcell2 is mutcell1
 
+def test_no_mutcell_if_immutable():
+    # don't introduce an immutable cell if the attribute seems immutable
+    from pypy.objspace.std.intobject import W_IntObject
+    cls = Class()
+    obj = cls.instantiate()
+    obj.setdictvalue(space, "a", W_IntObject(5))
+    assert not obj.map.ever_mutated
+
+    assert obj.getdictvalue(space, "a").intval == 5
+    mutcell = obj._mapdict_read_storage(0)
+    assert mutcell.intval == 5
+
 
 def test_mutcell_unwrap_only_if_needed():
     from pypy.objspace.std.intobject import W_IntObject


More information about the pypy-commit mailing list