]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129005: _pyio.BufferedIO remove copy on readall (#129454)
authorCody Maloney <cmaloney@users.noreply.github.com>
Thu, 30 Jan 2025 11:23:25 +0000 (03:23 -0800)
committerGitHub <noreply@github.com>
Thu, 30 Jan 2025 11:23:25 +0000 (11:23 +0000)
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.

Lib/_pyio.py
Misc/NEWS.d/next/Library/2025-01-29-00-00-01.gh-issue-129005.aV_3O8.rst [new file with mode: 0644]

index 76a27910da4d5f375508881b565373dc523e9bfe..755e025877089136af068a8edafc1070ed08b3dd 100644 (file)
@@ -1062,6 +1062,9 @@ class BufferedReader(_BufferedIOMixin):
                 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 (file)
index 0000000..48ee571
--- /dev/null
@@ -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.