Skip to content

Commit 7e0f7ae

Browse files
committed
pythongh-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.
1 parent 41ad2bb commit 7e0f7ae

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

Lib/_pyio.py

+3
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,9 @@ def _read_unlocked(self, n=None):
10621062
if chunk is None:
10631063
return buf[pos:] or None
10641064
else:
1065+
# Avoid slice + copy if there is no data in buf
1066+
if not buf:
1067+
return chunk
10651068
return buf[pos:] + chunk
10661069
chunks = [buf[pos:]] # Strip the consumed bytes.
10671070
current_size = 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove an unnecessary copy when ``_pyio.BufferedReader.read`` is called to
2+
read all data from a file and has no data already in buffer.

0 commit comments

Comments
 (0)