summaryrefslogtreecommitdiffstats
path: root/Modules/bz2module.c
diff options
context:
space:
mode:
authorNadeem Vawda <nadeem.vawda@gmail.com>2011-10-13 11:34:16 (GMT)
committerNadeem Vawda <nadeem.vawda@gmail.com>2011-10-13 11:34:16 (GMT)
commitd41a98bdd9076eeadf4d3ba6d8db287e26b89777 (patch)
tree41e6908d7d713d1981060eded58e10c00098850c /Modules/bz2module.c
parentf1ab47ebc4d6f6cdb0ea7f9ec73d874aae03a1f2 (diff)
downloadcpython-d41a98bdd9076eeadf4d3ba6d8db287e26b89777.zip
cpython-d41a98bdd9076eeadf4d3ba6d8db287e26b89777.tar.gz
cpython-d41a98bdd9076eeadf4d3ba6d8db287e26b89777.tar.bz2
Issue #13159: Replace FileIO's quadratic-time buffer growth algorithm with a linear-time one.
Also fix the bz2 module, whose classes used the same algorithm.
Diffstat (limited to 'Modules/bz2module.c')
-rw-r--r--Modules/bz2module.c19
1 files changed, 4 insertions, 15 deletions
diff --git a/Modules/bz2module.c b/Modules/bz2module.c
index 3e55202..a671e8d 100644
--- a/Modules/bz2module.c
+++ b/Modules/bz2module.c
@@ -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(). */