[pypy-commit] pypy default: improve zipfile performance by not doing repeated string concatenation

planter pypy.commits at gmail.com
Sun May 28 13:36:57 EDT 2017


Author: Petre Vijiac <petre.vijiac at rinftech.com>
Branch: 
Changeset: r91434:bed189ed5900
Date: 2017-05-28 19:35 +0200
http://bitbucket.org/pypy/pypy/changeset/bed189ed5900/

Log:	improve zipfile performance by not doing repeated string
	concatenation

	(committed by cfbolz)

diff --git a/lib-python/2.7/zipfile.py b/lib-python/2.7/zipfile.py
--- a/lib-python/2.7/zipfile.py
+++ b/lib-python/2.7/zipfile.py
@@ -622,19 +622,23 @@
         """Read and return up to n bytes.
         If the argument is omitted, None, or negative, data is read and returned until EOF is reached..
         """
-        buf = ''
+        # PyPy modification: don't do repeated string concatenation
+        buf = []
+        lenbuf = 0
         if n is None:
             n = -1
         while True:
             if n < 0:
                 data = self.read1(n)
-            elif n > len(buf):
-                data = self.read1(n - len(buf))
+            elif n > lenbuf:
+                data = self.read1(n - lenbuf)
             else:
-                return buf
+                break
             if len(data) == 0:
-                return buf
-            buf += data
+                break
+            lenbuf += len(data)
+            buf.append(data)
+        return "".join(buf)
 
     def _update_crc(self, newdata, eof):
         # Update the CRC using the given data.


More information about the pypy-commit mailing list