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 #129424 · python/cpython

Conversation

@cmaloney

@cmaloney cmaloney commented Jan 29, 2025 •
edited
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 added 2 commits January 28, 2025 21:19
@cmaloney
pythongh-129005: Align FileIO.readall allocation
f4a8de5
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.3 seconds
@cmaloney
add blurb
871979d
@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 force-pushed the cmaloney/pyio_fileio_readall branch from e382660 to 871979d Compare January 29, 2025 05:40
cmaloney added 2 commits January 28, 2025 22:35
@cmaloney
Update 2025-01-28-21-22-44.gh-issue-129005.h57i9j.rst
5cee34e
@cmaloney
Merge branch 'main' into cmaloney/pyio_fileio_readall
6c010f2
cmaloney
cmaloney commented Jan 29, 2025
View reviewed changes
Comment thread Lib/_pyio.py Resolved

Uh oh!

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

cmaloney and others added 2 commits January 29, 2025 13:59
@cmaloney
Remove unneecssary variable
869a31b
@cmaloney
Merge branch 'main' into cmaloney/pyio_fileio_readall
188b239
@cmaloney

cmaloney commented Jan 29, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

Not sure how to get the Tests / Ubuntu / build and test unbroken, they seem to be auto-cancelling fast... which isn't happening on any of my other open PRs.

All reactions

Sorry, something went wrong.

Uh oh!

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

@cmaloney cmaloney closed this Jan 29, 2025
@cmaloney

cmaloney commented Jan 29, 2025

Copy link
Copy Markdown
Contributor Author

rebased on main and opened #129458 as a replacement that hopefully will revoid the cancelled test runs.

All reactions

Sorry, something went wrong.

Uh oh!

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

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

Reviewers

No reviews

Uh oh!

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

Assignees

No one assigned

Labels

awaiting review

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.

1 participant

@cmaloney
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 01402503 — 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.