Author: Antonio Cuni <[email protected]>
Branch: faster-rstruct-2
Changeset: r91187:3542b2943610
Date: 2017-05-05 18:44 +0200
http://bitbucket.org/pypy/pypy/changeset/3542b2943610/

Log:    implement typed_read for BytearrayBuffer and test that struct.unpack
        takes the fast path. However this is sub-optimal, see the comment
        for an explanation

diff --git a/pypy/module/struct/test/test_struct.py 
b/pypy/module/struct/test/test_struct.py
--- a/pypy/module/struct/test/test_struct.py
+++ b/pypy/module/struct/test/test_struct.py
@@ -507,3 +507,8 @@
         buf = self.struct.pack("iii", 0, 42, 43)
         offset = self.struct.calcsize("i")
         assert self.struct.unpack_from("ii", buf, offset) == (42, 43)
+
+    def test_unpack_bytearray(self):
+        buf = self.struct.pack("iii", 0, 42, 43)
+        buf = bytearray(buf)
+        assert self.struct.unpack("iii", buf) == (0, 42, 43)
diff --git a/pypy/objspace/std/bytearrayobject.py 
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -1251,6 +1251,18 @@
     def get_raw_address(self):
         return nonmoving_raw_ptr_for_resizable_list(self.data)
 
+    @specialize.ll_and_arg(1)
+    def typed_read(self, TP, byte_offset):
+        # XXX: this is sub-efficient, because it forces the bytearray to
+        # become non-movable in order to do the raw_load. In theory, it should
+        # be possible to do the same using llop.gc_load_indexed, the same way
+        # we do it for strings. However, we cannot do it because there is no
+        # way to convert self.data from being a high-level list into the ll
+        # equivalent.
+        from rpython.rtyper.lltypesystem.lloperation import llop
+        raw_ptr = self.get_raw_address()
+        return llop.raw_load(TP, raw_ptr, byte_offset)
+
 
 @specialize.argtype(1)
 def _memcmp(selfvalue, buffer, length):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to