Skip to content

return more data at once from TlsStream::poll_read - #198

Merged
djc merged 3 commits into
rustls:mainfrom
trinity-1686a:hyper-perf
Sep 2, 2026
Merged

djc merged 3 commits into
rustls:mainfrom
trinity-1686a:hyper-perf

Conversation

@trinity-1686a

Copy link
Copy Markdown
Contributor

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 running perf stat ./target/release/examples/client https://proof.ovh.net/files/1Gb.dat (download 1GiB from a remote server), i get

cpu-cycles:u
before: 3.6B
after: 2.7B

task-clock:u
before: 11.7s
after: 7.3s

While 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.

this can improve the performance of some consumers, notably Hyper

@djc djc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Comment thread src/client.rs Resolved
Comment thread src/client.rs Resolved
@djc

djc commented Sep 1, 2026

Copy link
Copy Markdown
Member

Would be cool to also do this for the server side.

@trinity-1686a

Copy link
Copy Markdown
Contributor Author

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 trinity-1686a changed the title return more data at once from ClientTlsStream::poll_read return more data at once from TlsStream::poll_read Sep 1, 2026
@djc
djc merged commit c0fad2f into rustls:main Sep 2, 2026
11 checks passed
@rdettai-sk

rdettai-sk commented Oct 7, 2026 •

Copy link
Copy Markdown

@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:
image

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

@djc

djc commented Oct 7, 2026

Copy link
Copy Markdown
Member

Sorry for the regression!

Yielding every once in a while sounds like a good idea, want to submit a PR?

@trinity-1686a

trinity-1686a commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

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

@rdettai-sk

rdettai-sk commented Oct 7, 2026 •

Copy link
Copy Markdown

Sorry Dirkjan if we are hijacking this thread to discuss QW stuff 😅

Do you see regression on qps/query latency in quickwit?

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.

@trinity-1686a

Copy link
Copy Markdown
Contributor Author
Version target Hyper polls     Avg      p90      p99     p100  Summed time
0.26.4     ovh      107145  29.4us   59.3us   71.3us  277.2us    3152.9 ms
0.26.5     ovh       68992  42.0us  133.5us  169.3us  444.1us    2896.2 ms
0.26.4      s3       19318  24.4us   58.3us   97.0us  267.2us      471.2ms
0.26.5      s3        5510  71.7us  192.9us  367.2us  622.6us      395.2ms

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)

@rdettai-sk

Copy link
Copy Markdown

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?

@trinity-1686a

Copy link
Copy Markdown
Contributor Author

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)

Are you sure the 400k byte is a strict cap on hyper?

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)

Do you think we should yielding every 128KB or so?

in the context of hyper, i don't think it's necessary. In the context of other tokio-rustls consumers, maybe?

@djc

djc commented Oct 7, 2026

Copy link
Copy Markdown
Member

Of course tokio-rustls is not just for use in hyper, so it feels like we should definitely add some kind of limit.

rdettai-sk added a commit to rdettai-sk/tokio-rustls that referenced this pull request Oct 7, 2026
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]>
@rdettai-sk

rdettai-sk commented Oct 9, 2026 •

Copy link
Copy Markdown

In quickwit we're joining tons of futures, so it's likely that our tasks are just polling too many different streams without yielding

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants