Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a3f4457

Browse files
committed
Speed up reading of small files. This avoids multiple C read() calls on pyc files.
1 parent 006917e commit a3f4457

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

Modules/_io/fileio.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ new_buffersize(fileio *self, size_t currentsize
572572
#endif
573573
)
574574
{
575+
size_t addend;
575576
#ifdef HAVE_FSTAT
576577
if (end != (Py_off_t)-1) {
577578
/* Files claiming a size smaller than SMALLCHUNK may
@@ -589,9 +590,16 @@ new_buffersize(fileio *self, size_t currentsize
589590
}
590591
#endif
591592
/* Expand the buffer by an amount proportional to the current size,
592-
giving us amortized linear-time behavior. Use a less-than-double
593-
growth factor to avoid excessive allocation. */
594-
return currentsize + (currentsize >> 3) + 6;
593+
giving us amortized linear-time behavior. For bigger sizes, use a
594+
less-than-double growth factor to avoid excessive allocation. */
595+
if (currentsize > 65536)
596+
addend = currentsize >> 3;
597+
else
598+
addend = 256 + currentsize;
599+
if (addend < SMALLCHUNK)
600+
/* Avoid tiny read() calls. */
601+
addend = SMALLCHUNK;
602+
return addend + currentsize;
595603
}
596604

597605
static PyObject *

0 commit comments

Comments
 (0)