[pypy-commit] pypy faster-rstruct-2: add tests for the fast paths of struct.unpack on raw buffers and bytearrays

antocuni pypy.commits at gmail.com
Sun May 14 19:05:12 EDT 2017


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: faster-rstruct-2
Changeset: r91289:4c03a04236a1
Date: 2017-05-15 00:55 +0200
http://bitbucket.org/pypy/pypy/changeset/4c03a04236a1/

Log:	add tests for the fast paths of struct.unpack on raw buffers and
	bytearrays

diff --git a/pypy/module/pypyjit/test_pypy_c/test_struct.py b/pypy/module/pypyjit/test_pypy_c/test_struct.py
--- a/pypy/module/pypyjit/test_pypy_c/test_struct.py
+++ b/pypy/module/pypyjit/test_pypy_c/test_struct.py
@@ -105,3 +105,44 @@
                 i90 = gc_load_indexed_i(p88, 0, 1, _, -4)
                 i91 = gc_load_indexed_i(p88, 4, 1, _, -4)
             """)
+
+    def test_unpack_raw_buffer(self):
+        def main(n):
+            import array
+            import struct
+            buf = struct.pack('H', 0x1234)
+            buf = array.array('b', buf)
+            i = 1
+            res = 0
+            while i < n:
+                val = struct.unpack("h", buf)[0]     # ID: unpack
+                res += val
+                i += 1
+            return res
+        log = self.run(main, [1000])
+        assert log.result == main(1000)
+        loop, = log.loops_by_filename(self.filepath)
+        assert loop.match_by_id('unpack', """
+            guard_not_invalidated(descr=...)
+            i65 = raw_load_i(i49, 0, descr=<ArrayS 2>)
+        """)
+
+    def test_unpack_bytearray(self):
+        def main(n):
+            import struct
+            buf = struct.pack('H', 0x1234)
+            buf = bytearray(buf)
+            i = 1
+            res = 0
+            while i < n:
+                val = struct.unpack("h", buf)[0]     # ID: unpack
+                res += val
+                i += 1
+            return res
+        log = self.run(main, [1000])
+        assert log.result == main(1000)
+        loop, = log.loops_by_filename(self.filepath)
+        assert loop.match_by_id('unpack', """
+            guard_not_invalidated(descr=...)
+            i70 = gc_load_indexed_i(p48, 0, 1, _, -2)
+        """)


More information about the pypy-commit mailing list