gh-129005: Remove copies from _pyio using take_bytes (#141539)

Memory usage now matches that of _io for large files.
This commit is contained in:
Cody Maloney 2025-11-18 01:10:32 -08:00 committed by GitHub
parent 4867f717e2
commit 58f3fe0d9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 8 additions and 9 deletions

View file

@ -546,7 +546,7 @@ def nreadahead():
res += b
if res.endswith(b"\n"):
break
return bytes(res)
return res.take_bytes()
def __iter__(self):
self._checkClosed()
@ -620,7 +620,7 @@ def read(self, size=-1):
if n < 0 or n > len(b):
raise ValueError(f"readinto returned {n} outside buffer size {len(b)}")
del b[n:]
return bytes(b)
return b.take_bytes()
def readall(self):
"""Read until EOF, using multiple read() call."""
@ -628,7 +628,7 @@ def readall(self):
while data := self.read(DEFAULT_BUFFER_SIZE):
res += data
if res:
return bytes(res)
return res.take_bytes()
else:
# b'' or None
return data
@ -1738,7 +1738,7 @@ def readall(self):
assert len(result) - bytes_read >= 1, \
"os.readinto buffer size 0 will result in erroneous EOF / returns 0"
result.resize(bytes_read)
return bytes(result)
return result.take_bytes()
def readinto(self, buffer):
"""Same as RawIOBase.readinto()."""