Commits

Antonio Cuni committed db54acd

keeping track of the lengths of writes is not enough: we want to make sure
that also the descrs are the same, in case e.g. I store a long and then read a
double (in that case, we just abort the optimization)

Comments (0)

Files changed (2)

pypy/jit/metainterp/optimizeopt/rawbuffer.py

         # overlaps
         self.offsets = []
         self.lengths = []
+        self.descrs = []
         self.values = []
 
     def _get_memory(self):
         NOT_RPYTHON
         for testing only
         """
-        return zip(self.offsets, self.lengths, self.values)
+        return zip(self.offsets, self.lengths, self.descrs, self.values)
 
-    def write_value(self, offset, length, value):
+    def write_value(self, offset, length, descr, value):
         i = 0
         N = len(self.offsets)
         while i < N:
             if self.offsets[i] == offset:
-                if length != self.lengths[i]:
+                if length != self.lengths[i] or descr != self.descrs[i]:
+                    # in theory we could add support for the cases in which
+                    # the lenght or descr is different, but I don't think we
+                    # need it in practice
                     raise InvalidRawWrite
                 # update the value at this offset
-                self.offsets[i] = offset
-                self.lengths[i] = length
                 self.values[i] = value
                 return
             elif self.offsets[i] > offset:
         # insert a new value at offset
         self.offsets.insert(i, offset)
         self.lengths.insert(i, length)
+        self.descrs.insert(i, descr)
         self.values.insert(i, value)
 
-    def read_value(self, offset, length):
+    def read_value(self, offset, length, descr):
         i = 0
         N = len(self.offsets)
         while i < N:
             if self.offsets[i] == offset:
-                if length != self.lengths[i]:
+                if length != self.lengths[i] or descr != self.descrs[i]:
                     raise InvalidRawRead
                 return self.values[i]
             i += 1

pypy/jit/metainterp/optimizeopt/test/test_rawbuffer.py

 
 def test_write_value():
     buf = RawBuffer()
-    buf.write_value(8, 4, 'three')
-    buf.write_value(0, 4, 'one')
-    buf.write_value(4, 2, 'two')
-    buf.write_value(12, 2, 'four')
+    buf.write_value(8, 4, 'descr3', 'three')
+    buf.write_value(0, 4, 'descr1', 'one')
+    buf.write_value(4, 2, 'descr2', 'two')
+    buf.write_value(12, 2, 'descr4', 'four')
     assert buf._get_memory() == [
-        ( 0, 4, 'one'),
-        ( 4, 2, 'two'),
-        ( 8, 4, 'three'),
-        (12, 2, 'four'),
+        ( 0, 4, 'descr1', 'one'),
+        ( 4, 2, 'descr2', 'two'),
+        ( 8, 4, 'descr3', 'three'),
+        (12, 2, 'descr4', 'four'),
         ]
     #
 
 def test_write_value_update():
     buf = RawBuffer()
-    buf.write_value(0, 4, 'one')
-    buf.write_value(4, 2, 'two')
-    buf.write_value(0, 4, 'ONE')
+    buf.write_value(0, 4, 'descr', 'one')
+    buf.write_value(4, 2, 'descr', 'two')
+    buf.write_value(0, 4, 'descr', 'ONE')
     assert buf._get_memory() == [
-        ( 0, 4, 'ONE'),
-        ( 4, 2, 'two'),
+        ( 0, 4, 'descr', 'ONE'),
+        ( 4, 2, 'descr', 'two'),
         ]
 
 def test_write_value_invalid_length():
     buf = RawBuffer()
-    buf.write_value(0, 4, 'one')
+    buf.write_value(0, 4, 'descr1', 'one')
     with py.test.raises(InvalidRawWrite):
-        buf.write_value(0, 5, 'two')
+        buf.write_value(0, 5, 'descr1', 'two')
+    with py.test.raises(InvalidRawWrite):
+        buf.write_value(0, 4, 'descr2', 'two')
+
     
 def test_write_value_overlapping():
     buf = RawBuffer()
-    buf.write_value(0, 4, 'one')
-    buf.write_value(6, 4, 'two')
+    buf.write_value(0, 4, 'descr', 'one')
+    buf.write_value(6, 4, 'descr', 'two')
     with py.test.raises(InvalidRawWrite):
-        buf.write_value(4, 4, 'three')
+        buf.write_value(4, 4, 'descr', 'three')
 
 def test_read_value():
     buf = RawBuffer()
-    buf.write_value(0, 4, 'one')
-    buf.write_value(4, 4, 'two')
-    assert buf.read_value(0, 4) == 'one'
-    assert buf.read_value(4, 4) == 'two'
+    buf.write_value(0, 4, 'descr', 'one')
+    buf.write_value(4, 4, 'descr', 'two')
+    assert buf.read_value(0, 4, 'descr') == 'one'
+    assert buf.read_value(4, 4, 'descr') == 'two'
     with py.test.raises(InvalidRawRead):
-        buf.read_value(0, 2)
+        buf.read_value(0, 2, 'descr')
     with py.test.raises(InvalidRawRead):
-        buf.read_value(8, 2)
+        buf.read_value(8, 2, 'descr')
+    with py.test.raises(InvalidRawRead):
+        buf.read_value(0, 4, 'another descr')
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.