hyperium / hyper

An HTTP library for Rust
https://hyper.rs
MIT License
14.62k stars 1.6k forks source link

perf(http1): improve parsing of sequentially partial messages #3764

Closed seanmonstar closed 1 month ago

seanmonstar commented 1 month ago

If request headers are received in incremental partial chunks, hyper would restart parsing each time. This is because the HTTP/1 parser is stateless, since the most common case is a full message and stateless parses faster.

However, if continuing to receive more partial chunks of the request, each subsequent full parse is slower and slower. Since partial parses is less common, we can store a little bit of state to improve performance in general.

Now, if a partial request is received, hyper will check for the end of the message quickly, and if not found, simply save the length to allow the next partial chunk to start its search from there. Only once the end is found will a fill parse happen.

This issue was reported by Datong Sun of Kong Inc.

seanmonstar commented 1 month ago

cc @dndx

dndx commented 1 month ago

Thanks for the prompt fix @seanmonstar !