Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Optimize :meth:`!io.BufferedReader.readline` when lines are close to the
buffer size (128 kB by default) or longer than the buffer size. Replace the
C loop searching for the newline byte in the buffer with a :c:func:`!memchr`
call which is more efficient. In the good case, it makes ``readline()`` up to
4x faster! Patch by Victor Stinner.
16 changes: 7 additions & 9 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1280,16 +1280,14 @@ _buffered_readline(buffered *self, Py_ssize_t limit)
n = limit;
}
start = self->buffer;
const char *end = start + n;
s = start;
while (s < end) {
if (*s++ == '\n') {
if (PyBytesWriter_WriteBytes(writer, start, s - start) < 0) {
goto error;
}
self->pos = s - start;
goto found;
const char *newline = memchr(start, '\n', n);
if (newline != NULL) {
n = newline - start + 1;
if (PyBytesWriter_WriteBytes(writer, start, n) < 0) {
goto error;
}
self->pos = n;
goto found;
}

if (PyBytesWriter_WriteBytes(writer, start, n) < 0) {
Expand Down
Loading