From 7e0f7aed8ced3be54fb03871f0db3e73b2005f51 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Wed, 29 Jan 2025 00:00:05 -0800 Subject: [PATCH 1/3] gh-129005: _pyio.BufferedIO remove copy on readall 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 | 3 +++ .../Library/2025-01-29-00-00-01.gh-issue-129005.aV_3O8.rst | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-01-29-00-00-01.gh-issue-129005.aV_3O8.rst diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 023478aa78c6a0..baa797972faa4d 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..4ee0da516241ab --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-29-00-00-01.gh-issue-129005.aV_3O8.rst @@ -0,0 +1,2 @@ +Remove an unnecessary copy when ``_pyio.BufferedReader.read`` is called to +read all data from a file and has no data already in buffer. From fe251e8b1941e84a08e05d453d47029153e71725 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 30 Jan 2025 11:54:28 +0100 Subject: [PATCH 2/3] Update Misc/NEWS.d/next/Library/2025-01-29-00-00-01.gh-issue-129005.aV_3O8.rst --- .../Library/2025-01-29-00-00-01.gh-issue-129005.aV_3O8.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index 4ee0da516241ab..33289a5f2b97a1 100644 --- 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 @@ -1,2 +1,2 @@ -Remove an unnecessary copy when ``_pyio.BufferedReader.read`` is called to -read all data from a file and has no data already in buffer. +: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. From 8f52a54afc6655af78ff1d93d6a265b74063baca Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 30 Jan 2025 11:59:57 +0100 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2025-01-29-00-00-01.gh-issue-129005.aV_3O8.rst --- .../next/Library/2025-01-29-00-00-01.gh-issue-129005.aV_3O8.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 33289a5f2b97a1..48ee57109be2ff 100644 --- 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 @@ -1,2 +1,2 @@ -:mod:`_pyio`: Remove an unnecessary copy when ``_pyio.BufferedReader.read()`` +: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.