You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
proto/h1: spurious copy-allocation in request parsing hotpath #3574
I was looking at the memory allocation patterns of a hyper-1.0 HTTP1 server with a fastwebsocket endpoint (basically equivalent to this example) and I noticed that there is a spurious memory copy-allocation in the uri.parse()? URI parsing logic here:
changed the title [-]proto/h1: spurious allocation in request parsing hotpath[/-][+]proto/h1: spurious copy-allocation in request parsing hotpath[/+]on Feb 19, 2024
After looking a bit into this request-parsing logic, it seems to me that the underlying problem is that the httparse crate does not make use of memory sharing through the bytes crate. It returns a fully parsed &str instead. So this codepath is not constructing an Uri from a shared Bytes, but doing a full memory copy instead.
I suspect this may require porting httparse to use the bytes crate for sharing the existing memory content.
Ah, this was change actually was a performance improvement back when I did it: Bytes back then had an inline variant, and fitting / into that variant was faster than an atomic clone. Of course, Bytes no longer has that variant.
Changing it back isn't impossible, it'd be similar to how the headers are cloned: the &str can be turned into indices into the original buffer, and then we can slice from the Bytes.
@seanmonstar thanks for the hint, I didn't notice there was already some &str -> Bytes dance. I've followed the same approach for the path URI, PR at #3575.
There are both a pipeline/hello_world_16 macro-benchmark and a bench_parse_incoming micro-benchmark related to this logic.
I run several master-vs-PR benches and the numbers are comparable, here are the runs with the smallest variation intervals observed on my workstation:
Activity
[-]proto/h1: spurious allocation in request parsing hotpath[/-][+]proto/h1: spurious copy-allocation in request parsing hotpath[/+]lucab commentedon Feb 19, 2024
After looking a bit into this request-parsing logic, it seems to me that the underlying problem is that the
httparsecrate does not make use of memory sharing through thebytescrate. It returns a fully parsed&strinstead. So this codepath is not constructing anUrifrom a sharedBytes, but doing a full memory copy instead.I suspect this may require porting
httparseto use thebytescrate for sharing the existing memory content.seanmonstar commentedon Feb 19, 2024
Ah, this was change actually was a performance improvement back when I did it:
Bytesback then had an inline variant, and fitting/into that variant was faster than an atomic clone. Of course,Bytesno longer has that variant.Changing it back isn't impossible, it'd be similar to how the headers are cloned: the
&strcan be turned into indices into the original buffer, and then we can slice from theBytes.seanmonstar commentedon Feb 19, 2024
Would also be easy-ish to see if the
pipelinebenchmarks improve with the change.lucab commentedon Feb 20, 2024
@seanmonstar thanks for the hint, I didn't notice there was already some
&str -> Bytesdance. I've followed the same approach for the path URI, PR at #3575.There are both a
pipeline/hello_world_16macro-benchmark and abench_parse_incomingmicro-benchmark related to this logic.I run several master-vs-PR benches and the numbers are comparable, here are the runs with the smallest variation intervals observed on my workstation:
perf(http1): avoid copy-allocation in request path parsing (#3575)