gh-150942: Speed up BytesIO.readlines() using reference-stealing append - #155171
Open
Taeknology wants to merge 4 commits into
Open
gh-150942: Speed up BytesIO.readlines() using reference-stealing append#155171Taeknology wants to merge 4 commits into
Taeknology wants to merge 4 commits into
Conversation
Taeknology
marked this pull request as ready for review
August 4, 2026 15:14
eendebakpt
reviewed
Aug 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Use
_PyList_AppendTakeRef()while building the private result list inBytesIO.readlines().The new bytes object is transferred directly to the result list, avoiding the
incref/decref pair performed by
PyList_Append(). The result list is freshlyallocated, remains local for the complete loop, and is protected by the
BytesIOcritical section until it is returned.Partially fixes gh-150942.
Benchmark
Author-run
pyperfcomparisons used fiveBytesIO.readlines()workloads on afree-threaded Clang 21 PGO+ThinLTO build. Two balanced baseline/patch comparison
pairs reported improvements of 8.64% to 36.18% across the workloads, with
19.27% and 20.43% equal-weight geometric-mean improvements. Earlier regular-GIL
PGO+LTO comparisons reported 7.64% and 8.68% geometric-mean improvements.
Several individual suites produced
pyperf checkstability warnings. The rawbenchmark script and result files are not attached to this PR, so these figures
are author-reported results and are not independently reproducible from the
public PR artifacts.
Verification
All required GitHub checks pass on the current head, including regular and
free-threaded builds and tests, sanitizer jobs, and generated-file checks.
git diff --checkalso passes for the complete two-file diff.