[Python-checkins] cpython: In text I/O, optimize scanning for new lines with 1-byte unicode chars

antoine.pitrou python-checkins at python.org
Sun Nov 13 04:20:16 CET 2011


http://hg.python.org/cpython/rev/6a357b949df1
changeset:   73538:6a357b949df1
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sun Nov 13 03:53:42 2011 +0100
summary:
  In text I/O, optimize scanning for new lines with 1-byte unicode chars

files:
  Modules/_io/textio.c |  32 ++++++++++++++++++++------------
  1 files changed, 20 insertions(+), 12 deletions(-)


diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -365,19 +365,23 @@
             */
             if (seennl == 0 &&
                 memchr(in_str, '\n', kind * len) != NULL) {
-                Py_ssize_t i = 0;
-                for (;;) {
-                    Py_UCS4 c;
-                    /* Fast loop for non-control characters */
-                    while (PyUnicode_READ(kind, in_str, i) > '\n')
-                        i++;
-                    c = PyUnicode_READ(kind, in_str, i++);
-                    if (c == '\n') {
-                        seennl |= SEEN_LF;
-                        break;
+                if (kind == PyUnicode_1BYTE_KIND)
+                    seennl |= SEEN_LF;
+                else {
+                    Py_ssize_t i = 0;
+                    for (;;) {
+                        Py_UCS4 c;
+                        /* Fast loop for non-control characters */
+                        while (PyUnicode_READ(kind, in_str, i) > '\n')
+                            i++;
+                        c = PyUnicode_READ(kind, in_str, i++);
+                        if (c == '\n') {
+                            seennl |= SEEN_LF;
+                            break;
+                        }
+                        if (i >= len)
+                            break;
                     }
-                    if (i >= len)
-                        break;
                 }
             }
             /* Finished: we have scanned for newlines, and none of them
@@ -1597,6 +1601,10 @@
 static char *
 find_control_char(int kind, char *s, char *end, Py_UCS4 ch)
 {
+    if (kind == PyUnicode_1BYTE_KIND) {
+        assert(ch < 256);
+        return (char *) memchr((void *) s, (char) ch, end - s);
+    }
     for (;;) {
         while (PyUnicode_READ(kind, s, 0) > ch)
             s += kind;

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


More information about the Python-checkins mailing list