Merge #13159: Replace FileIO's quadratic-time buffer growth algorithm with a linear-time one.
Also fix the bz2 module, which suffered from the same problem.
diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c
index d329c14..b407df9 100644
--- a/Modules/_bz2module.c
+++ b/Modules/_bz2module.c
@@ -116,22 +116,14 @@
#define SMALLCHUNK BUFSIZ
#endif
-#if SIZEOF_INT < 4
-#define BIGCHUNK (512 * 32)
-#else
-#define BIGCHUNK (512 * 1024)
-#endif
-
static int
grow_buffer(PyObject **buf)
{
+ /* 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. */
size_t size = PyBytes_GET_SIZE(*buf);
- if (size <= SMALLCHUNK)
- return _PyBytes_Resize(buf, size + SMALLCHUNK);
- else if (size <= BIGCHUNK)
- return _PyBytes_Resize(buf, size * 2);
- else
- return _PyBytes_Resize(buf, size + BIGCHUNK);
+ return _PyBytes_Resize(buf, size + (size >> 3) + 6);
}