mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
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.
This commit is contained in:
commit
72d6a13413
3 changed files with 11 additions and 27 deletions
|
@ -116,22 +116,14 @@ catch_bz2_error(int bzerror)
|
|||
#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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -43,12 +43,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;
|
||||
|
@ -572,15 +566,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 *
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue