Skip to content

Conversation

@ruiyangke
Copy link
Contributor

@ruiyangke ruiyangke commented Feb 1, 2026

Fix incorrect type check in buffered_iternext() that prevented the fast path from ever being taken, slowing down iteration over buffered binary files.

Since tp is already a type object, Py_IS_TYPE(tp, X) expands to Py_TYPE(tp) == X, which compares the type itself against BufferedReader. This is always false, so the fast path was never taken.

pyperf benchmark:

+----------------+---------+-----------------------+
| Benchmark      | buggy   | fixed                 |
+================+=========+=======================+
| iteration      | 3.05 ms | 2.04 ms: 1.49x faster |
+----------------+---------+-----------------------+

Closes: #144380

@bedevere-app
Copy link

bedevere-app bot commented Feb 1, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot
Copy link

python-cla-bot bot commented Feb 1, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@colesbury
Copy link
Contributor

Thanks for catching this. Would you please add a news entry (blurb)?

@colesbury colesbury added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Feb 1, 2026
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`.
@ruiyangke ruiyangke force-pushed the fix/io/bufferedio-iternext-type-check branch from 6230d1a to 257b345 Compare February 1, 2026 23:35
@ruiyangke
Copy link
Contributor Author

Thanks for catching this. Would you please add a news entry (blurb)?

Thanks, new entry added

@kumaraditya303 kumaraditya303 merged commit 40d07ca into python:main Feb 2, 2026
55 checks passed
@miss-islington-app
Copy link

Thanks @ruiyangke for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 2, 2026
…ythonGH-144381)

(cherry picked from commit 40d07cad38bf3ce60f4ca03f1836e8650fe40df5)

Co-authored-by: Ruiyang Ke <me@ry.ke>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 2, 2026
…ythonGH-144381)

(cherry picked from commit 40d07ca)

Co-authored-by: Ruiyang Ke <me@ry.ke>
@bedevere-app
Copy link

bedevere-app bot commented Feb 2, 2026

GH-144389 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Feb 2, 2026
@bedevere-app
Copy link

bedevere-app bot commented Feb 2, 2026

GH-144390 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Feb 2, 2026
kumaraditya303 pushed a commit that referenced this pull request Feb 2, 2026
…H-144381) (#144390)

gh-144380: Fix incorrect type check in `buffered_iternext()` (GH-144381)
(cherry picked from commit 40d07ca)

Co-authored-by: Ruiyang Ke <me@ry.ke>
kumaraditya303 pushed a commit that referenced this pull request Feb 2, 2026
…H-144381) (#144389)

gh-144380: Fix incorrect type check in `buffered_iternext()` (GH-144381)
(cherry picked from commit 40d07ca)

Co-authored-by: Ruiyang Ke <me@ry.ke>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fast path in buffered_iternext is never taken due to incorrect type check

4 participants