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-129005: Align FileIO.readall allocation by cmaloney · Pull Request #129458 · python/cpython

Conversation

@cmaloney

@cmaloney cmaloney commented Jan 29, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Both _io and _pyio now use a pre-allocated buffer of length bufsize, fill it using a os.readinto / _Py_read, and have matching "expand buffer" logic.

On my machine (Linux, Debug build) this takes: ./python -m test -M8g -uall test_largefile -m test_large_read -v from ~3.7 seconds to ~3.3 seconds

_pyio still uses 2x the memory, there are two remaining copies

  1. the bytes(result) currently copies. I'd like to either just rely on "duck typing" / bytearray is close enough to bytes, or would need to do something similar to C++ "move" semantics where the bytes could take ownership of the data buffer from the bytearray without copying... Not sure what is most Pythonic
  2. _pyio.BufferedIO._read_unlocked in the read-all case where no buffer has been allocated always does return buf[:pos] + chunk which causes another copy. Patch for that case / "if buf is length 0, just return" coming shortly.
  • Issue: Reduce copies when reading files in pyio, match behavior of _io #129005

Sorry, something went wrong.

Uh oh!

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

All reactions
@cmaloney
pythongh-129005: Align FileIO.readall allocation
96078b0
Both now use a pre-allocated buffer of length `bufsize`, fill it using
a readinto, and have matching "expand buffer" logic.

On my machine this takes:

`./python -m test -M8g -uall test_largefile -m test_large_read -v`
from ~3.7 seconds to ~3.4 seconds
@bedevere-app bedevere-app Bot added the awaiting review label Jan 29, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Jan 29, 2025
Reduce copies when reading files in pyio, match behavior of _io #129005
Closed
@cmaloney cmaloney mentioned this pull request Jan 29, 2025
gh-129005: Align FileIO.readall allocation #129424
Closed
vstinner
vstinner reviewed Jan 30, 2025
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2025-01-28-21-22-44.gh-issue-129005.h57i9j.rst Resolved

Uh oh!

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

@vstinner
Update Misc/NEWS.d/next/Library/2025-01-28-21-22-44.gh-issue-129005.h…
044ec50
…57i9j.rst
@vstinner
vstinner enabled auto-merge (squash) January 30, 2025 10:50
@vstinner

vstinner commented Jan 30, 2025

Copy link
Copy Markdown
Member

LGTM. I enabled auto-merge.

cmaloney reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

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

@vstinner
vstinner merged commit f927204 into python:main Jan 30, 2025
@bedevere-app bedevere-app Bot removed the awaiting review label Jan 30, 2025
@cmaloney
cmaloney deleted the cmalnoey/pyio_fileio_readall_2 branch January 30, 2025 21:57
cmaloney added a commit to cmaloney/cpython that referenced this pull request Feb 2, 2025
@cmaloney
Revert "pythongh-129005: Align FileIO.readall() allocation (python#12…
eb3aa4b
…9458)"

This reverts commit f927204.
vstinner pushed a commit that referenced this pull request Feb 2, 2025
@cmaloney
Revert "gh-129005: Align FileIO.readall() allocation (#129458)" (#129572
853a6b7
)

This reverts commit f927204.
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Feb 7, 2025
@cmaloney @srinivasreddy
pythongh-129005: Align FileIO.readall() allocation (python#129458)
3ae74d6
Both now use a pre-allocated buffer of length `bufsize`, fill it using
a readinto(), and have matching "expand buffer" logic.

On my machine this takes:

`./python -m test -M8g -uall test_largefile -m test_large_read -v`

from ~3.7 seconds to ~3.4 seconds.
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Feb 7, 2025
@cmaloney @srinivasreddy
Revert "pythongh-129005: Align FileIO.readall() allocation (python#12…
cee0ebc
…9458)" (python#129572)

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

Reviewers

@vstinner vstinner vstinner left review comments

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

2 participants

@cmaloney @vstinner
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.