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.
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
--- /dev/null
+: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.