Skip to content

Conversation

@ruiyangke
Copy link

@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
Author

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

Thanks, new entry added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

3 participants