Skip to content

Commit 257b345

Browse files
committed
gh-144380: Fix incorrect type check in buffered_iternext()
The condition used Py_IS_TYPE(tp, ...) where tp is already a `type`, this made the fast path unreachable. Avoid unnecessary method resolution checks in the iteration hot path, resulting in ~49% faster line iteration for `io.BufferedReader`.
1 parent b625601 commit 257b345

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve performance of :class:`io.BufferedReader` line iteration by ~49%.

Modules/_io/bufferedio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,8 +1505,8 @@ buffered_iternext(PyObject *op)
15051505

15061506
_PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
15071507
tp = Py_TYPE(self);
1508-
if (Py_IS_TYPE(tp, state->PyBufferedReader_Type) ||
1509-
Py_IS_TYPE(tp, state->PyBufferedRandom_Type))
1508+
if (tp == state->PyBufferedReader_Type ||
1509+
tp == state->PyBufferedRandom_Type)
15101510
{
15111511
/* Skip method call overhead for speed */
15121512
line = _buffered_readline(self, -1);

0 commit comments

Comments
 (0)