Author: Antonio Cuni <[email protected]>
Branch: faster-rstruct-2
Changeset: r91284:a8366709a997
Date: 2017-05-13 15:27 +0200
http://bitbucket.org/pypy/pypy/changeset/a8366709a997/

Log:    now that we have all the necessary infrastructure, implement
        pack_into in a more efficient way, so that PackFormatIterator writes
        directly inside the destination buffer (using the fast paths if
        possible)

diff --git a/pypy/module/struct/formatiterator.py 
b/pypy/module/struct/formatiterator.py
--- a/pypy/module/struct/formatiterator.py
+++ b/pypy/module/struct/formatiterator.py
@@ -35,7 +35,8 @@
     @jit.unroll_safe
     def align(self, mask):
         pad = (-self.pos) & mask
-        self.wbuf.setzeros(self.pos, pad)
+        for i in range(self.pos, self.pos+pad):
+            self.wbuf.setitem(i, '\x00')
         self.advance(pad)
 
     def finished(self):
diff --git a/pypy/module/struct/interp_struct.py 
b/pypy/module/struct/interp_struct.py
--- a/pypy/module/struct/interp_struct.py
+++ b/pypy/module/struct/interp_struct.py
@@ -59,22 +59,28 @@
     return space.newbytes(_pack(space, format, args_w))
 
 
-# XXX inefficient
 @unwrap_spec(format='text', offset=int)
 def pack_into(space, format, w_buffer, offset, args_w):
     """ Pack the values v1, v2, ... according to fmt.
 Write the packed bytes into the writable buffer buf starting at offset
     """
-    res = _pack(space, format, args_w)
+    size = _calcsize(space, format)
     buf = space.getarg_w('w*', w_buffer)
     if offset < 0:
         offset += buf.getlength()
-    size = len(res)
     if offset < 0 or (buf.getlength() - offset) < size:
         raise oefmt(get_error(space),
                     "pack_into requires a buffer of at least %d bytes",
                     size)
-    buf.setslice(offset, res)
+    #
+    wbuf = SubBuffer(buf, offset, size)
+    fmtiter = PackFormatIterator(space, wbuf, args_w)
+    try:
+        fmtiter.interpret(format)
+    except StructOverflowError as e:
+        raise OperationError(space.w_OverflowError, space.newtext(e.msg))
+    except StructError as e:
+        raise OperationError(get_error(space), space.newtext(e.msg))
 
 
 def _unpack(space, format, buf):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to