GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-150942: Speed up IOBase.readlines by demitryfly · Pull Request #155304 · python/cpython

Conversation

@demitryfly

@demitryfly demitryfly commented Aug 6, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

This PR optimizes _io.IOBase.readlines() by using
_PyList_AppendTakeRef() when adding lines to the result list.

Previously, each line was added with PyList_Append(), which creates an
extra temporary reference. The new code transfers the existing reference
directly to the result list, avoiding an unnecessary reference-count
increment/decrement pair.

The order of operations is unchanged: the line is added before its length
is inspected, preserving the prior behavior.

Tests:

  • ./python.exe -m test --verbose test_io
  • Issue: Improve performance by using reference stealing methods #150942

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@python-cla-bot

python-cla-bot Bot commented Aug 6, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app

bedevere-app Bot commented Aug 6, 2026

Copy link
Copy Markdown

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the awaiting review label Aug 6, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Aug 6, 2026
Improve performance by using reference stealing methods #150942
Open
@demitryfly
pythongh-150942: Speed up IOBase.readlines
c75bcab
@demitryfly
demitryfly force-pushed the gh-150942-iobase-readlines branch from f2b4ed4 to c75bcab Compare August 6, 2026 21:49
@Eclips4
Merge branch 'main' into pythongh-150942-iobase-readlines
12880e0
@Eclips4
Eclips4 requested a review from eendebakpt August 11, 2026 04:53
eendebakpt
eendebakpt reviewed Aug 11, 2026
View reviewed changes
Comment thread Modules/_io/iobase.c

if (PyList_Append(result, line) < 0) {
Py_DECREF(line);
if (_PyList_AppendTakeRef((PyListObject *)result, line) < 0) {

@eendebakpt eendebakpt Aug 11, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to PyObject_Size(line) should be moved up. After the _PyList_AppendTakeRef we are no longer have a valid reference to line.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/_io/iobase.c
/* XXX special-casing this made sense in the Python version in order
to remove the bytecode interpretation overhead, but it could
probably be removed here. */
PyObject *ret = PyObject_CallMethodObjArgs(result, &_Py_ID(extend),

@eendebakpt eendebakpt Aug 11, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in this PR will not matter for calls like f.readlines() since that ends up in the fast path here. We could change the call here to PyObject_CallMethodOneArg which might be faster, but that is for another PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Misc/NEWS.d/next/Library/2026-08-07-00-40-00.gh-issue-150942.demitryfly.rst
@@ -0,0 +1,2 @@
Improve the performance of :meth:`io.IOBase.readlines` by avoiding

@eendebakpt eendebakpt Aug 11, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide benchmarks where this change has a positive effect for real-world cases? I suspect the change might be too minimal to warrant acceptance of the PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

StanFromIreland reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@demitryfly demitryfly Oct 27, 2026

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benchmark showed that this change has no significant effect, so I’ve decided to close this PR.

Thanks for the review!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@StanFromIreland StanFromIreland added the pending The issue will be closed if no feedback is provided label Aug 15, 2026
@demitryfly demitryfly closed this Oct 27, 2026
@demitryfly
demitryfly deleted the gh-150942-iobase-readlines branch October 27, 2026 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@eendebakpt eendebakpt eendebakpt left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review pending The issue will be closed if no feedback is provided

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@demitryfly @eendebakpt @StanFromIreland @Eclips4
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.