gh-141968: Use take_bytes to remove copy in _pyio.BytesIO.read()#149850
gh-141968: Use take_bytes to remove copy in _pyio.BytesIO.read()#149850lgeiger wants to merge 3 commits into
take_bytes to remove copy in _pyio.BytesIO.read()#149850Conversation
| b = self._buffer[self._pos : newpos] | ||
| self._pos = newpos | ||
| return bytes(b) | ||
| return b.take_bytes() |
There was a problem hiding this comment.
As far as I can tell b = self._buffer[self._pos : newpos] already copies the bytearray so we can safely use take_bytes()
|
cc @cmaloney |
cmaloney
left a comment
There was a problem hiding this comment.
I think your analysis is correct / the slice already does a copy so take_bytes() is safe in this case.
I've been experimenting to see if there is a way to avoid the temporary bytearray / go straight from the original to final bytes but haven't found anything simpler so far (would save allocating the bytearray head). Can swap in a memoryview but I don't find that simpler, just a different shape.
Could you remove the news file? I added skip news co PR checks should pass. This is just an optimizing refactor and _pyio is not widely used so people are unlikely to see an impact from this. End users only see the C _io implementation. The CPython test suite will run a little faster though which is nice :)
also: How did you spot/find this one? (Wondering if there's more tips and tricks should add to the whatsnew)
This removes a copy going from
bytearraytobytesin_pyio.BytesIO.read().