[pypy-commit] pypy pypy_bytearray: bytearray performance fix(ported from PyPy3)

smihnea pypy.commits at gmail.com
Wed Jul 19 10:15:45 EDT 2017


Author: Mihnea Saracin <mihnea.saracin at rinftech.com>
Branch: pypy_bytearray
Changeset: r91922:b43a6e2c0ea1
Date: 2017-07-19 13:25 +0300
http://bitbucket.org/pypy/pypy/changeset/b43a6e2c0ea1/

Log:	bytearray performance fix(ported from PyPy3)

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
@@ -567,24 +567,16 @@
             raise
     else:
         return list(buf.as_str())
+    return _from_byte_sequence(space, w_source)
 
-    # sequence of bytes
-    w_iter = space.iter(w_source)
-    length_hint = space.length_hint(w_source, 0)
-    data = newlist_hint(length_hint)
-    extended = 0
-    while True:
-        try:
-            w_item = space.next(w_iter)
-        except OperationError as e:
-            if not e.match(space, space.w_StopIteration):
-                raise
-            break
-        data.append(space.byte_w(w_item))
-        extended += 1
-    if extended < length_hint:
-        resizelist_hint(data, extended)
-    return data
+def _from_byte_sequence(space, w_source):
+    # Split off in a separate function for the JIT's benefit
+    w_result = space.appexec([w_source], """(seq):
+        result = bytearray()
+        for i in seq:
+            result.append(i)
+        return result""")
+    return w_result.getdata()
 
 
 def _hex_digit_to_int(d):


More information about the pypy-commit mailing list