Repository navigation
return more data at once from TlsStream::poll_read - #198
Conversation
this can improve the performance of some consumers, notably Hyper
|
Would be cool to also do this for the server side. |
136c31f to
40cdf85
Compare
|
i've confirmed the effect on the read path of the server. i should also add because it bite me while trying to assess performance on the server half that, in hyper, this only impacts http1, h2 there seem to use much smaller buffers so that results are largely within the variance of my setup |
|
@trinity-1686a did you measure the impact of your change on the poll durations? I have deployed it on a busy Quickwit deployment: as expected, the number of polls decreased drastically (10x) but the number of slow polls exploded: Do you think we should yield from time to time in case hyper's buffer is huge? Edit: it's not just the distribution that moved because because of fewer very short poll (that could explain the graphs above). We really have more slow polls: Polls of 16–256 ms went from 0.15 to 2.5/s per searcher |
|
Sorry for the regression! Yielding every once in a while sounds like a good idea, want to submit a PR? |
|
i haven't measured individual poll duration (will do). Hyper doesn't read more than ~400KB per poll, it shouldn't take that long to fill the buffer. In quickwit we're joining tons of futures, so it's likely that our tasks are just polling too many different streams without yielding, and these tasks taking a bit longer now moved us from okayish to really long poll. Do you see regression on qps/query latency in quickwit? In our tests, we didn't have noticeable change on those, but a gain in average cpu usage |
|
Sorry Dirkjan if we are hijacking this thread to discuss QW stuff 😅
I noticed it because I have been working on the stability of the runtime, trying to offload everything that was triggering slow polls. I finally got to something pretty good, where only S3 was still using a lot of poll time but with few slow polls. I was trying hunt down why S3 downloads were using so much CPU time and this is when I tested this PR. I got no noticeable performance improvement (though as you now the CPU baseline is very noisy), but I immediately noticed that my runtime got a lot less stable. |
poll are expectedly longer, but nowhere near the multi ms increase you see, so i think that's an artefact of how quickwit works (and doesn't yield enough) |
|
The join explanation holds up. The 4x drop in poll count is pretty far from what I see (10x). I assume hyper buffers are bigger if the bandwidth is higher. 2.5x larger buffers + 10x joins and you get in the same range as what I'm seeing Are you sure the 400k byte is a strict cap on hyper? Do you think we should yielding every 128KB or so? |
|
the exact drop is very dependent on factors like network conditions, and even how busy the cpu is (it's one of those optimisation that actually get better under load: you get scheduled less often, so the kernel has more to give you at once)
408kiB is hyper default limit for its read buffer, you can override it, quickwit doesn't (the description of the const is in term of "header bigger than this error out", but the same limit, and buffer, is used to read header and body)
in the context of hyper, i don't think it's necessary. In the context of other tokio-rustls consumers, maybe? |
|
Of course tokio-rustls is not just for use in hyper, so it feels like we should definitely add some kind of limit. |
Clamping the last chunk saves no decryption: the record is already decrypted by the time it is copied. Overshooting by less than one record keeps the loop identical to the one from rustls#198 apart from its condition. Co-Authored-By: Claude Opus 5.5 <[email protected]>
actually hyper spawns its own tasks for connections, so I don't think this explanation holds (joins might trigger other slow polls though) EDIT: confirmed making the warmup joins cooperative. |

When using Hyper with tokio-rustls, and streaming a response body, hyper only emits blocks of 16k (a single TLS record), even when hyper uses a larger receive buffer. This causes more wake-ups for hyper callers, and generally more work inside hyper itself.
This PR makes it so TlsStream::poll_read tries to return more data at once, which measurably improves Hyper's performance over TLS streams.
I assume there is a similar gain on the server side with requests that have a large body (instead of response having a body for client), though i haven't attempted to measure or fix that yet.
very simple (but noisy) bench setup:
running a slightly simplified version of this example (commenting out
to_bytes()and actual printing of the body, to make the gain more visible), and runningperf stat ./target/release/examples/client https://proof.ovh.net/files/1Gb.dat(download 1GiB from a remote server), i getWhile this could also be a change in hyper to call poll_read() until it returns Pending, i think other consumers of rustls could have the same inefficiency, and this is better handled at that level.