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.
This commit is contained in:
Cody Maloney 2025-01-30 03:23:25 -08:00 committed by GitHub
parent 3bebe46d34
commit e1c4ba9288
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 0 deletions

View 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