[Python-checkins] r68504 - sandbox/trunk/io-c/_textio.c

antoine.pitrou python-checkins at python.org
Sat Jan 10 21:16:14 CET 2009


Author: antoine.pitrou
Date: Sat Jan 10 21:16:14 2009
New Revision: 68504

Log:
Speedup TextIOWrapper.__init__ by bypassing method call overhead in the common case.
Good for a 50% speedup in "for line in f: pass"



Modified:
   sandbox/trunk/io-c/_textio.c

Modified: sandbox/trunk/io-c/_textio.c
==============================================================================
--- sandbox/trunk/io-c/_textio.c	(original)
+++ sandbox/trunk/io-c/_textio.c	Sat Jan 10 21:16:14 2009
@@ -926,17 +926,12 @@
 
 
 static PyObject *
-TextIOWrapper_readline(PyTextIOWrapperObject *self, PyObject *args)
+_TextIOWrapper_readline(PyTextIOWrapperObject *self, Py_ssize_t limit)
 {
-    Py_ssize_t limit = -1;
     PyObject *line;
     Py_ssize_t start, endpos;
     int res;
 
-    if (!PyArg_ParseTuple(args, "|n:readline", &limit)) {
-        return NULL;
-    }
-
     if (_PyIOBase_checkClosed((PyObject *)self, Py_True) == NULL)
         return NULL;
 
@@ -1069,6 +1064,17 @@
     return NULL;
 }
 
+static PyObject *
+TextIOWrapper_readline(PyTextIOWrapperObject *self, PyObject *args)
+{
+    Py_ssize_t limit = -1;
+
+    if (!PyArg_ParseTuple(args, "|n:readline", &limit)) {
+        return NULL;
+    }
+    return _TextIOWrapper_readline(self, limit);
+}
+
 /* Seek and Tell */
 
 #if defined(MS_WIN64) || defined(MS_WINDOWS)
@@ -1573,7 +1579,15 @@
 
     self->telling = 0;
 
-    line = PyObject_CallMethod((PyObject *)self, "readline", NULL);
+    if (Py_TYPE(self) == &PyTextIOWrapper_Type) {
+        /* Skip method call overhead for speed */
+        line = _TextIOWrapper_readline(self, -1);
+    }
+    else {
+        line = PyObject_CallMethodObjArgs((PyObject *)self,
+                                           _PyIO_str_readline, NULL);
+        /* TODO: check return type */
+    }
 
     if (line == NULL)
         return NULL;
@@ -1581,6 +1595,7 @@
     assert (PyUnicode_Check(line));
 
     if (PyUnicode_GET_SIZE(line) == 0) {
+        /* Reached EOF or would have blocked */
         Py_DECREF(line);
         Py_CLEAR(self->snapshot);
         self->telling = self->seekable;


More information about the Python-checkins mailing list