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: Remove copies from _pyio using take_bytes by cmaloney · Pull Request #141539 · python/cpython

Conversation

@cmaloney

@cmaloney cmaloney commented Nov 13, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Memory usage now matches that of _io for large files.

$ ./python -Wd -I -m test test_io.test_largefile -uall,largefile,walltime -M32G -v
== CPython 3.15.0a1+ (heads/gh129005_take_bytes-dirty:d8b88498ebf, Nov 13 2025, 15:43:51) [Clang 21.1.5 ]
== Linux-6.17.7-arch1-1-x86_64-with-glibc2.42 little-endian
== Python build: debug
== cwd: <build_dir>/build/test_python_worker_116910æ
== CPU count: 32
== encodings: locale=UTF-8 FS=utf-8
== resources: all

Using random seed: 365280675
0:00:00 load avg: 9.60 Run 1 test sequentially in a single process
0:00:00 load avg: 9.60 [1/1] test_io.test_largefile
test_large_read (test.test_io.test_largefile.CLargeFileTest.test_large_read) ... 
 ... expected peak memory use: 2.3G
 ... process data size: 2.3G
ok
test_lseek (test.test_io.test_largefile.CLargeFileTest.test_lseek) ... ok
test_osstat (test.test_io.test_largefile.CLargeFileTest.test_osstat) ... ok
test_seek_read (test.test_io.test_largefile.CLargeFileTest.test_seek_read) ... ok
test_seek_readall (test.test_io.test_largefile.CLargeFileTest.test_seek_readall) ... 
 ... expected peak memory use: 2.3G
 ... process data size: 2.3G
 ... process data size: 2.3G
ok
test_seekable (test.test_io.test_largefile.CLargeFileTest.test_seekable) ... ok
test_truncate (test.test_io.test_largefile.CLargeFileTest.test_truncate) ... ok
test_large_read (test.test_io.test_largefile.PyLargeFileTest.test_large_read) ... 
 ... expected peak memory use: 2.3G
 ... process data size: 2.3G
ok
test_lseek (test.test_io.test_largefile.PyLargeFileTest.test_lseek) ... ok
test_osstat (test.test_io.test_largefile.PyLargeFileTest.test_osstat) ... ok
test_seek_read (test.test_io.test_largefile.PyLargeFileTest.test_seek_read) ... ok
test_seek_readall (test.test_io.test_largefile.PyLargeFileTest.test_seek_readall) ... 
 ... expected peak memory use: 2.3G
 ... process data size: 2.3G
 ... process data size: 2.3G
ok
test_seekable (test.test_io.test_largefile.PyLargeFileTest.test_seekable) ... ok
test_truncate (test.test_io.test_largefile.PyLargeFileTest.test_truncate) ... ok
test_it (test.test_io.test_largefile.TestCopyfile.test_it) ... ok
test_it (test.test_io.test_largefile.TestSocketSendfile.test_it) ... ok

----------------------------------------------------------------------
Ran 16 tests in 6.950s

OK
0:00:06 load avg: 9.72 [1/1] test_io.test_largefile passed

== Tests result: SUCCESS ==

1 test OK.

Total duration: 7.0 sec
Total tests: run=16
Total test files: run=1/1
Result: SUCCESS

For reference; this test goes from 8.5sec overall duration on my AMD 64 bit Arch Linux box to 7 seconds.

  • 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: Remove copy using take_bytes from _pyio
d8b8849
Memory usage now matches that of _io for large files
@bedevere-app bedevere-app Bot added the awaiting review label Nov 13, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Nov 13, 2025
Reduce copies when reading files in pyio, match behavior of _io #129005
Closed
@cmaloney cmaloney added the skip news label Nov 13, 2025
@cmaloney

cmaloney commented Nov 14, 2025

Copy link
Copy Markdown
Contributor Author

Optimization enabled by gh-139871 (Trying to keep that one issue referencing all the things which I use it for; planning to expand the What's New with a single "here's all the modules that got faster" list once optimize)

All reactions

Sorry, something went wrong.

Uh oh!

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

@cmaloney cmaloney changed the title gh-129005: Remove copy from _pyio using take_bytes gh-129005: Remove copies from _pyio using take_bytes Nov 14, 2025
@cmaloney
Merge branch 'main' into gh129005_take_bytes
0346363
@cmaloney cmaloney mentioned this pull request Nov 15, 2025
gh-60107: Remove a copy from RawIOBase.read #141532
Merged
vstinner
vstinner reviewed Nov 17, 2025
View reviewed changes
Comment thread Lib/_pyio.py
@@ -620,15 +620,15 @@ def read(self, size=-1):
if n < 0 or n > len(b):
raise ValueError(f"readinto returned {n} outside buffer size {len(b)}")
del b[n:]

@vstinner vstinner Nov 17, 2025

Copy link
Copy Markdown
Member

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.

Is there a difference between b.resize(n) and del b[n:]?

Sorry, something went wrong.

Uh oh!

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

All reactions

@cmaloney cmaloney Nov 18, 2025

Copy link
Copy Markdown
Contributor 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.

.resize() could grow while the del b[n:] never will. There's some code which does a = bytearray(b'12'); del a[3:] (intentionally del a slice past the end) as part of its buffer management. Generally I prefer resize because there's a lot less code involved than the slice code (and the slice code eventually calls resize anyways)

Sorry, something went wrong.

Uh oh!

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

vstinner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
vstinner
vstinner approved these changes Nov 17, 2025
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

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.

LGTM

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Nov 17, 2025
@vstinner
vstinner merged commit 58f3fe0 into python:main Nov 18, 2025
48 checks passed

Uh oh!

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

@bedevere-app bedevere-app Bot removed the awaiting merge label Nov 18, 2025
@cmaloney
cmaloney deleted the gh129005_take_bytes branch November 18, 2025 22:10
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
@cmaloney @StanFromIreland
pythongh-129005: Remove copies from _pyio using take_bytes (python#14…
4b4776a
…1539)

Memory usage now matches that of _io for large files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

skip news

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 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.