]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #13159: Replace FileIO's quadratic-time buffer growth algorithm with a linear...
authorNadeem Vawda <nadeem.vawda@gmail.com>
Thu, 13 Oct 2011 11:34:16 +0000 (13:34 +0200)
committerNadeem Vawda <nadeem.vawda@gmail.com>
Thu, 13 Oct 2011 11:34:16 +0000 (13:34 +0200)
Also fix the bz2 module, whose classes used the same algorithm.

Misc/NEWS
Modules/_io/fileio.c
Modules/bz2module.c

index 186ea2145cfb4d6b8a080cf19a61a6d4d79269e9..3c3bff826417232d1f157230dcad786402f02a61 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -121,6 +121,9 @@ Tests
 Extension Modules
 -----------------
 
+- Issue #13159: FileIO and BZ2File now use a linear-time buffer growth
+  strategy instead of a quadratic-time one.
+
 - Issue #13070: Fix a crash when a TextIOWrapper caught in a reference cycle
   would be finalized after the reference to its underlying BufferedRWPair's
   writer got cleared by the GC.
index b1d492b7a341d96192ecbb090832e028c1548545..be5c9f8da9774b3535b74a7649ba64efaa036288 100644 (file)
 #define SMALLCHUNK BUFSIZ
 #endif
 
-#if SIZEOF_INT < 4
-#define BIGCHUNK  (512 * 32)
-#else
-#define BIGCHUNK  (512 * 1024)
-#endif
-
 typedef struct {
     PyObject_HEAD
     int fd;
@@ -565,15 +559,10 @@ new_buffersize(fileio *self, size_t currentsize)
         }
     }
 #endif
-    if (currentsize > SMALLCHUNK) {
-        /* Keep doubling until we reach BIGCHUNK;
-           then keep adding BIGCHUNK. */
-        if (currentsize <= BIGCHUNK)
-            return currentsize + currentsize;
-        else
-            return currentsize + BIGCHUNK;
-    }
-    return currentsize + SMALLCHUNK;
+    /* Expand the buffer by an amount proportional to the current size,
+       giving us amortized linear-time behavior. Use a less-than-double
+       growth factor to avoid excessive allocation. */
+    return currentsize + (currentsize >> 3) + 6;
 }
 
 static PyObject *
index 3e55202bd8bae9ed25e42a36056bb47509057321..a671e8d9e6b87e2c2c56f20e42038f8450b8ec14 100644 (file)
@@ -218,25 +218,14 @@ Util_CatchBZ2Error(int bzerror)
 #define SMALLCHUNK BUFSIZ
 #endif
 
-#if SIZEOF_INT < 4
-#define BIGCHUNK  (512 * 32)
-#else
-#define BIGCHUNK  (512 * 1024)
-#endif
-
 /* This is a hacked version of Python's fileobject.c:new_buffersize(). */
 static size_t
 Util_NewBufferSize(size_t currentsize)
 {
-    if (currentsize > SMALLCHUNK) {
-        /* Keep doubling until we reach BIGCHUNK;
-           then keep adding BIGCHUNK. */
-        if (currentsize <= BIGCHUNK)
-            return currentsize + currentsize;
-        else
-            return currentsize + BIGCHUNK;
-    }
-    return currentsize + SMALLCHUNK;
+    /* Expand the buffer by an amount proportional to the current size,
+       giving us amortized linear-time behavior. Use a less-than-double
+       growth factor to avoid excessive allocation. */
+    return currentsize + (currentsize >> 3) + 6;
 }
 
 /* This is a hacked version of Python's fileobject.c:get_line(). */