You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
_io.FileIO uses a quadratic-time buffer growth algorithm #57368
As mentioned in bpo-6715, the buffer growth strategy used by _io.FileIO
has quadratic running time if each reallocation is O(n). The code in
question is new_buffersize() from Modules/_io/fileio.c:
if (currentsize>SMALLCHUNK) {
/* Keep doubling until we reach BIGCHUNK; then keep adding BIGCHUNK. */if (currentsize <= BIGCHUNK)
returncurrentsize+currentsize;
elsereturncurrentsize+BIGCHUNK;
}
returncurrentsize+SMALLCHUNK;
Is there a reason for this? One possible improvement would be to instead
use the same formula as list.resize() in Objects/listobject.c:
Activity
nadeemvawda commentedon Oct 12, 2011
As mentioned in bpo-6715, the buffer growth strategy used by _io.FileIO
has quadratic running time if each reallocation is O(n). The code in
question is new_buffersize() from Modules/_io/fileio.c:
Is there a reason for this? One possible improvement would be to instead
use the same formula as list.resize() in Objects/listobject.c:
which has amortized O(n) running time behaviour.
Your thoughts?
nadeemvawda commentedon Oct 12, 2011
I've attached a patch that makes the suggested change to FileIO, and also
to _bz2.BZ2Compressor/Decompressor (which currently have the same issue).
pitrou commentedon Oct 12, 2011
The patch looks good to me, thanks.
python-dev commentedon Oct 13, 2011
New changeset d18c80a8c119 by Nadeem Vawda in branch '3.2':
Issue bpo-13159: Replace FileIO's quadratic-time buffer growth algorithm with a linear-time one.
http://hg.python.org/cpython/rev/d18c80a8c119
New changeset 4a6709a071d0 by Nadeem Vawda in branch 'default':
Merge bpo-13159: Replace FileIO's quadratic-time buffer growth algorithm with a linear-time one.
http://hg.python.org/cpython/rev/4a6709a071d0
python-dev commentedon Oct 13, 2011
New changeset c1c434e30e06 by Nadeem Vawda in branch '2.7':
Issue bpo-13159: Replace FileIO's quadratic-time buffer growth algorithm with a linear-time one.
http://hg.python.org/cpython/rev/c1c434e30e06
pitrou commentedon Oct 13, 2011
Thank you :)
nadeemvawda commentedon Oct 13, 2011
No problem :)