diff options
| author | Nadeem Vawda <nadeem.vawda@gmail.com> | 2011-10-13 11:52:46 (GMT) |
|---|---|---|
| committer | Nadeem Vawda <nadeem.vawda@gmail.com> | 2011-10-13 11:52:46 (GMT) |
| commit | 36248154a9c47283d4760b5c66402119043bbba1 (patch) | |
| tree | f3ec72118045d7898555359353e9b0f1b062065d /Objects/fileobject.c | |
| parent | 40ab00233ef34703db57a0806294f8bc911af551 (diff) | |
| download | cpython-36248154a9c47283d4760b5c66402119043bbba1.zip cpython-36248154a9c47283d4760b5c66402119043bbba1.tar.gz cpython-36248154a9c47283d4760b5c66402119043bbba1.tar.bz2 | |
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.
Diffstat (limited to 'Objects/fileobject.c')
| -rw-r--r-- | Objects/fileobject.c | 19 |
1 files changed, 4 insertions, 15 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c index edd839e..737ebb7 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -992,12 +992,6 @@ file_isatty(PyFileObject *f) #define SMALLCHUNK BUFSIZ #endif -#if SIZEOF_INT < 4 -#define BIGCHUNK (512 * 32) -#else -#define BIGCHUNK (512 * 1024) -#endif - static size_t new_buffersize(PyFileObject *f, size_t currentsize) { @@ -1026,15 +1020,10 @@ new_buffersize(PyFileObject *f, size_t currentsize) /* Add 1 so if the file were to grow we'd notice. */ } #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; } #if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN |
