[pypy-commit] pypy faster-rstruct-2: whoo... finally reach the whole point of the branch: struct.pack_into(bytearray...) takes the fast path :)

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


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: faster-rstruct-2
Changeset: r91285:973fa84efaef
Date: 2017-05-13 15:39 +0200
http://bitbucket.org/pypy/pypy/changeset/973fa84efaef/

Log:	whoo... finally reach the whole point of the branch:
	struct.pack_into(bytearray...) takes the fast path :)

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
@@ -518,3 +518,10 @@
         data = self.struct.pack("iii", 0, 42, 43)
         buf = array.array('c', data)
         assert self.struct.unpack("iii", buf) == (0, 42, 43)
+
+    def test_pack_into_bytearray(self):
+        expected = self.struct.pack("ii", 42, 43)
+        buf = bytearray(len(expected))
+        self.struct.pack_into("ii", buf, 0, 42, 43)
+        assert buf == expected
+        
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
@@ -1252,18 +1252,31 @@
     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):
+    def _get_gc_data(self):
         from rpython.rtyper.lltypesystem import lltype, llmemory
-        from rpython.rtyper.lltypesystem.lloperation import llop
         ll_data = ll_for_resizable_list(self.data)
         ll_items = ll_data.items
         LIST = lltype.typeOf(ll_data).TO # rlist.LIST_OF(lltype.Char)
         base_ofs = llmemory.itemoffsetof(LIST.items.TO, 0)
         scale_factor = llmemory.sizeof(lltype.Char)
+        return ll_items, scale_factor, base_ofs
+
+    @specialize.ll_and_arg(1)
+    def typed_read(self, TP, byte_offset):
+        from rpython.rtyper.lltypesystem.lloperation import llop
+        ll_items, scale_factor, base_ofs = self._get_gc_data()
         return llop.gc_load_indexed(TP, ll_items, byte_offset,
                                     scale_factor, base_ofs)
 
+    @specialize.ll_and_arg(1)
+    def typed_write(self, TP, byte_offset, value):
+        from rpython.rtyper.lltypesystem import lltype
+        from rpython.rtyper.lltypesystem.lloperation import llop
+        ll_items, scale_factor, base_ofs = self._get_gc_data()
+        value = lltype.cast_primitive(TP, value)
+        return llop.gc_store_indexed(lltype.Void, ll_items, byte_offset, value,
+                                     scale_factor, base_ofs)
+
 
 
 @specialize.argtype(1)
diff --git a/rpython/rlib/buffer.py b/rpython/rlib/buffer.py
--- a/rpython/rlib/buffer.py
+++ b/rpython/rlib/buffer.py
@@ -240,3 +240,7 @@
     @specialize.ll_and_arg(1)
     def typed_read(self, TP, byte_offset):
         return self.buffer.typed_read(TP, byte_offset + self.offset)
+
+    @specialize.ll_and_arg(1)
+    def typed_write(self, TP, byte_offset, value):
+        return self.buffer.typed_write(TP, byte_offset + self.offset, value)
diff --git a/rpython/rlib/rstruct/standardfmttable.py b/rpython/rlib/rstruct/standardfmttable.py
--- a/rpython/rlib/rstruct/standardfmttable.py
+++ b/rpython/rlib/rstruct/standardfmttable.py
@@ -49,7 +49,7 @@
             do_pack_fastpath(fmtiter, value)
         except CannotWrite:
             if not ALLOW_SLOWPATH:
-                raise ValueErro("fastpath not taken :(")
+                raise ValueError("fastpath not taken :(")
             return False
         else:
             return True


More information about the pypy-commit mailing list