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

antoine.pitrou python-checkins at python.org
Thu Jan 8 16:54:38 CET 2009


Author: antoine.pitrou
Date: Thu Jan  8 16:54:38 2009
New Revision: 68406

Log:
Consistent improvements in IncrementalNewlineDecoder speed, especially the \n-only case.



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	Thu Jan  8 16:54:38 2009
@@ -94,7 +94,8 @@
 
 #define SEEN_CR   1
 #define SEEN_LF   2
-#define SEEN_CRLF 4 
+#define SEEN_CRLF 4
+#define SEEN_ALL (SEEN_CR | SEEN_LF | SEEN_CRLF)
 
 static PyObject *
 IncrementalNewlineDecoder_decode(PyNewLineDecoderObject *self, 
@@ -172,11 +173,23 @@
     {
         Py_UNICODE *in_str;
         Py_ssize_t in, len;
-        int seennl = 0;
+        int seennl = self->seennl;
+        int only_lf;
 
         in_str = PyUnicode_AS_UNICODE(output);
         len = PyUnicode_GET_SIZE(output);
+
+        /* If, up to now, newlines are consistently \n, do a quick check
+           for the \r *byte* with the libc's optimized memchr.
+           */
+        only_lf = ((seennl == SEEN_LF)
+                && !memchr(in_str, '\r', len * sizeof(Py_UNICODE)));
+
         if (!self->translate) {
+            if (seennl == SEEN_ALL)
+                goto endscan;
+            if (only_lf)
+                goto endscan;
             for (in = 0; in < len;) {
                 Py_UNICODE c = in_str[in++];
                 if (c >= 0x20)
@@ -191,9 +204,13 @@
                     else
                         seennl |= SEEN_CR;
                 }
+                if (seennl == SEEN_ALL)
+                    goto endscan;
             }
+        endscan:
+            ;
         }
-        else {
+        else if (!only_lf) {
             PyObject *translated = NULL;
             Py_UNICODE *out_str;
             Py_ssize_t out;


More information about the Python-checkins mailing list