[Python-checkins] cpython: Speed up reading of small files. This avoids multiple C read() calls on pyc

antoine.pitrou python-checkins at python.org
Tue Apr 17 13:55:12 CEST 2012


http://hg.python.org/cpython/rev/ed954d11dadd
changeset:   76374:ed954d11dadd
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Tue Apr 17 13:50:58 2012 +0200
summary:
  Speed up reading of small files.  This avoids multiple C read() calls on pyc files.

files:
  Modules/_io/fileio.c |  14 +++++++++++---
  1 files changed, 11 insertions(+), 3 deletions(-)


diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -572,6 +572,7 @@
 #endif
                )
 {
+    size_t addend;
 #ifdef HAVE_FSTAT
     if (end != (Py_off_t)-1) {
         /* Files claiming a size smaller than SMALLCHUNK may
@@ -589,9 +590,16 @@
     }
 #endif
     /* 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;
+       giving us amortized linear-time behavior.  For bigger sizes, use a
+       less-than-double growth factor to avoid excessive allocation. */
+    if (currentsize > 65536)
+        addend = currentsize >> 3;
+    else
+        addend = 256 + currentsize;
+    if (addend < SMALLCHUNK)
+        /* Avoid tiny read() calls. */
+        addend = SMALLCHUNK;
+    return addend + currentsize;
 }
 
 static PyObject *

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list