gh-150942: Speed up BytesIO.readlines() using reference-stealing append - #155171
Open
Taeknology wants to merge 3 commits into
Open
gh-150942: Speed up BytesIO.readlines() using reference-stealing append#155171Taeknology wants to merge 3 commits into
Taeknology wants to merge 3 commits into
Conversation
Taeknology
marked this pull request as ready for review
August 4, 2026 15:14
eendebakpt
reviewed
Aug 4, 2026
| @@ -0,0 +1,2 @@ | |||
| Improve the performance of :meth:`~io.IOBase.readlines` for | |||
| :class:`io.BytesIO`, particularly in free-threaded builds. | |||
Contributor
There was a problem hiding this comment.
Suggested change
| :class:`io.BytesIO`, particularly in free-threaded builds. | |
| :class:`io.BytesIO`. |
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 freshly allocated, remains local for the complete loop, and cannot be accessed concurrently before it is returned.Partially fixes gh-150942.
Benchmark
The benchmark uses the five
BytesIO.readlines()workload shapes proposed for this issue:Each result is rigorous pyperf pinned to CPU 2 and executed in baseline-patch-patch-baseline order. Two independent baseline/patch builds used the exact same baseline-generated PGO profile (SHA-256
f9e7f64605c3fb5353369c304f32dce491c4315cb1a2001ea560a9584fbfed26) with Clang 21, PGO, ThinLTO, and a free-threaded build fromce5ae29ef9b8ccb32683c922d135339311c36c9d.Same-variant geometric-mean drift was 0.43% for the baselines and 0.54% for the patched builds. Every workload was significantly faster in both comparisons.
Earlier independent regular-GIL PGO+LTO pairs measured 7.64% and 8.68% geometric-mean improvements, with 0.37% baseline and 0.59% patched-build drift.
pyperf checkreports stability warnings for several individual suites. The raw suites are therefore retained and the claim is based on two balanced independent comparisons, the shared PGO profile, low same-variant drift, and consistent significant direction across every workload—not on an unqualified stability claim.Benchmark script and command:
$ python bench_bytesio_readlines.py --rigorous --affinity 2 -o result.jsonTests
python -m test test_io -j2: 1,047 run, 28 skipped, success.python -m test test_io -j2: 1,047 run, 28 skipped, success.python -m test -R 3:3 test_io.test_memoryio: success, no leaks in tracked runs.git diff --check: success.make patchcheck: success; one changed file.