-
-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse from noncontiguous byte slices? #74
Comments
I ran into the same issue / misconception. I assumed I can just read from a socket into a buffer, pass this buffer into I think that misconception comes from the way the docs are worded: From this, I assumed that the If I understand the API correctly, the expectation is that the incoming bytes should be read into a buffer until Is this correct? If yes, maybe the docs could be clarified 🙂 |
Actually, dropping the |
That's how httparse achieves zero-copy. You'll have to have an array of bytes containing the full message, then the Response object uses slices of information from that initial buffer array. By doing this, it doesn't have to keep an inner state and can be faster than a non-zero-copy parser at the cost of you having to allocate that buffer array. |
For me, the annoyance is less that a successful |
I use a circular buffer too, and I always wished httparse could parse from multiple source slices. It might be possible to do it in a way that avoids copying, by making the parsed fields comprised of multiple slices in case they wrap ( What I do as a workaround is re-align the circular buffer contents in place before passing the buffer to httparse. This way any extra copying only occurs once per expected frame, as opposed to repeatedly copying the circular buffer into a temporary buffer for each parse attempt. |
I am building a HTTP server on top of tokio that needs to perform minimal memory allocations per client TCP connection, regardless of how many HTTP requests are received through the connection. Therefore I have chosen to use a fixed-size circular buffer to store the raw request data read from the wire, and now I am trying to use httparse to parse the request information. The problem I have run into is that the
Request.parse
function takes in a single &[u8], but because I'm using a circular buffer I have two slices - one for the bytes in the remainder of the buffer, and one (optionally) for the bytes which wrapped around to the front of the buffer. This two-buffer approach works very well with vectored IO reads, but not so well so far with httparse.At first I was hoping I that httparse's
Request
type would be persistent, so I could callparse
in turn for both of the slices. But that appears to not be how the API works - it expects the one slice to have all the data, and when you call it a second time the same data should still be present, only with more added to the end.Consequently, the only way I can find to use httparse today is to perform a copy of the data from the circular buffer into a secondary contiguous buffer. But the cost of such copying is potentially significant and I'd prefer to avoid it where possible. How feasible would it be to add some sort of
parse_vectored
function to httpparse which takes a&[std::io::IoSlice]
?The text was updated successfully, but these errors were encountered: