Read request bodies lazily, fix wsgi performance regressions#63
Merged
Conversation
With a large maximum (4GB) specified for http_body.read, we ended up with a very large allocation taking place in the host. In the case of this hostcall, at least, we do need to read the body in chunks for reasonable performance. This change fixes that by ensuring that we perform reads in chunks; additionally, we now use a BufferedReader over our own RawIOBase which allows us to lazily read the bodies until later in execution (or not at all if the request body isn't read by the handler).
erikrose
approved these changes
Mar 23, 2026
Member
erikrose
left a comment
There was a problem hiding this comment.
Looks great. Back to full speed (≈31fps) on my box. Thank you! :-D
| """ | ||
| return not self._closed | ||
|
|
||
| def readinto(self, b) -> int: |
Member
There was a problem hiding this comment.
I couldn't come up with a type for b either. :-)
Member
Author
There was a problem hiding this comment.
Ah, I was following a snippet that didn't have type annotations. Seems like maybe https://docs.python.org/3/library/collections.abc.html#collections.abc.Buffer might be the correct type for the protocol. I'm inclined to just leave it as this matches the definition provided by cpython internally (well, at least in https://github.com/python/cpython/blob/main/Lib/_pyio.py#L636).
Member
There was a problem hiding this comment.
Best not to prod the dragons. ;-) Buffer was my first choice as well, but it's not len()-able.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
With a large maximum (4GB) specified for http_body.read, we ended up with a very large allocation taking place in the host. In the case of this hostcall, at least, we do need to read the body in chunks for reasonable performance.
This change fixes that by ensuring that we perform reads in chunks; additionally, we now use a BufferedReader over our own RawIOBase which allows us to lazily read the bodies until later in execution (or not at all if the request body isn't read by the handler).