small optimization of BytesIOPayload.size #2129
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When posting a large body that you have as a
bytes
object, as per #2127 , the body should be passed as aio.BytesIO
object.But when this BytesIO is initialised on a bytes object that might be used somewhere else, calling
getbuffer
needs to copy the underlying data to return a writable memoryview.(the instantiating the BytesIO in the loop to avoid the caching done by BytesIO)
This is problematic beyond wasting 500ms when making a request: the event loop is actually locked during this memory copy, so an underlying aiohttp server won't be able to process any other pending requests.
But, calling
getvalue
instead of getbuffer will make some (weird) use-cases way worse than now:Calling
seek(0, SEEK_END)
avoid all this