https://github.com/python/cpython/commit/e1c4ba928852eac0b0e0bded1c314e3e36975286
commit: e1c4ba928852eac0b0e0bded1c314e3e36975286
branch: main
author: Cody Maloney <[email protected]>
committer: vstinner <[email protected]>
date: 2025-01-30T11:23:25Z
summary:
gh-129005: _pyio.BufferedIO remove copy on readall (#129454)
Slicing buf and appending chunk would always result in a copy. Commonly
in a readall() there is no already read data in buf, and the amount of
data read may be large, so the copy is expensive.
files:
A Misc/NEWS.d/next/Library/2025-01-29-00-00-01.gh-issue-129005.aV_3O8.rst
M Lib/_pyio.py
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 76a27910da4d5f..755e0258770891 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -1062,6 +1062,9 @@ def _read_unlocked(self, n=None):
if chunk is None:
return buf[pos:] or None
else:
+ # Avoid slice + copy if there is no data in buf
+ if not buf:
+ return chunk
return buf[pos:] + chunk
chunks = [buf[pos:]] # Strip the consumed bytes.
current_size = 0
diff --git
a/Misc/NEWS.d/next/Library/2025-01-29-00-00-01.gh-issue-129005.aV_3O8.rst
b/Misc/NEWS.d/next/Library/2025-01-29-00-00-01.gh-issue-129005.aV_3O8.rst
new file mode 100644
index 00000000000000..48ee57109be2ff
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-01-29-00-00-01.gh-issue-129005.aV_3O8.rst
@@ -0,0 +1,2 @@
+:mod:`!_pyio`: Remove an unnecessary copy when ``_pyio.BufferedReader.read()``
+is called to read all data from a file and has no data already in buffer.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]