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>
@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot x86-64 macOS 3.13 (tier-2) has failed when building commit 51fccc6.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1483/builds/1215) and take a look at the build logs.
  4. Check if the failure is related to this commit (51fccc6) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1483/builds/1215

Failed tests:

  • test.test_multiprocessing_spawn.test_processes

Failed subtests:

  • test_repr_rlock - test.test_multiprocessing_spawn.test_processes.WithProcessesTestLock.test_repr_rlock

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/Users/buildbot/buildarea/3.13.billenstein-macos/build/Lib/test/_test_multiprocessing.py", line 1493, in test_repr_rlock
    self.assertEqual('<RLock(SomeOtherThread, nonzero)>', repr(lock))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: '<RLock(SomeOtherThread, nonzero)>' != '<RLock(None, 0)>'
- <RLock(SomeOtherThread, nonzero)>
+ <RLock(None, 0)>

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

5 participants