Issue #13159: Replace FileIO's quadratic-time buffer growth algorithm with a linear-time one.
Also fix the builtin file class and the bz2 module, which used the same algorithm.
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 9f90448..0048240 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -42,12 +42,6 @@
#define SMALLCHUNK BUFSIZ
#endif
-#if SIZEOF_INT < 4
-#define BIGCHUNK (512 * 32)
-#else
-#define BIGCHUNK (512 * 1024)
-#endif
-
typedef struct {
PyObject_HEAD
int fd;
@@ -528,15 +522,10 @@
}
}
#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 *