Repository navigation
ws: get random masks in batches - #23595
Closed
mvanslobbe wants to merge 2 commits into
Closed
mvanslobbe wants to merge 2 commits into
mvanslobbe wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
Review of mask-pool boundaries and lifecycle, random-backend contracts, debug overrides, and benchmark integration found no blocking issues.
No open issues 🎉
What changed in this PR
This PR reduces WebSocket send overhead by batching random-byte generation and caching masks in the encoder.
Changes:
- Fetches random bytes in one TLS backend call while preserving debug entropy overrides.
- Adds a 256-byte mask pool, refilled every 64 WebSocket frames.
- Adds and documents a local socket-pair benchmark for four payload sizes.
| File | Description |
|---|---|
| tests/perf/wssend.c | Adds the WebSocket send benchmark. |
| tests/perf/README.md | Documents benchmark behavior and usage. |
| tests/perf/Makefile.inc | Registers the new benchmark. |
| lib/ws.c | Pools random bytes for outgoing frame masks. |
| lib/vtls/vtls.c | Removes the four-byte random-request assertion. |
| lib/rand.c | Requests the full random buffer in one TLS call. |
Review effort: Balanced
bagder
approved these changes
Oct 26, 2026
Member
|
@mvanslobbe can you take a look at the merge conflicts and force push? |
Every frame libcurl sends needs a new 4-byte mask. Each one was fetched with its own Curl_rand() call, and Curl_rand_bytes() fetches only four bytes per call to the TLS library, so sending small frames spent most of its CPU time in RAND_bytes(). - let Curl_rand_bytes() fill the whole buffer with one Curl_ssl_random() call, unless CURL_ENTROPY forces the bytes, and drop the debug assert that Curl_ssl_random() only gets four bytes - keep a 256-byte pool of random bytes in the websocket encoder and take each frame's mask from it, refilling it every 64 frames ./perf wssend, nanoseconds per frame, median of nine runs: frame gcc before/after clang before/after 16 B 1529 / 789 -48% 1501 / 772 -49% 125 B 1544 / 807 -48% 1511 / 786 -48% 1 KB 1646 / 899 -45% 1576 / 839 -47% 16 KB 3549 / 2741 -23% 3143 / 2342 -26%
mvanslobbe
force-pushed
the
perf/ws-mask-pool
branch
from
October 26, 2026 16:58
9498a22 to
d174a4a
Compare
Contributor
Author
|
@bagder Rebased onto master and force-pushed. The conflict was only the perf test list in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every WebSocket frame libcurl sends needs a new 4-byte mask. Each mask was fetched with its own
Curl_rand()call, andCurl_rand_bytes()asks the TLS library for only four bytes per call, so sending small frames spent most of its CPU time inRAND_bytes().The cost is per call, not per byte. Profiling
./perf wssendwith OpenSSL 3.0 on master, the random call is about half the time of a 16-byte frame, and almost none of that is producing the bytes: AES-CTR output is under 1% of samples. Most of it goes to parameter lookups by name (OSSL_PARAM_locate()andstrcmp()), rwlock lock and unlock, re-keying the CTR DRBG after each request, and aRAND_status()check thatossl_random()makes on every call here.Curl_rand_bytes()fills the whole buffer with oneCurl_ssl_random()call, unlessCURL_ENTROPYforces the bytes in a debug buildCurl_ssl_random()only gets four bytes is dropped; every backend's random function takes any lengthThis also adds
wssend, a perf test that upgrades a connection to WebSocket over a local socket pair, with no server involved, and sends frames withcurl_ws_send(). The first commit has only the test, so it can be run before and after the change../perf wssend 200000, the first commit against this branch,-O2, OpenSSL, median of 9 alternating runs, nanoseconds per frame.On GitHub Actions, building both commits from GitHub: https://github.com/mvanslobbe/curl-fork/actions/runs/40167468803
(source: https://github.com/mvanslobbe/curl-fork/tree/15dfe68cef)
Locally, i7-12700H, Linux, OpenSSL 3, pinned to one core:
The saving is about the same per frame whatever the payload size, so it shows most on small frames. On macOS the 16384-byte frames do not get faster, which I have not looked into.
The full test suite passes with ASan and UBSan on gcc and clang with OpenSSL, and on clang without TLS, as do the torture runs of the WebSocket tests.
AI disclosure
This change was written with the help of Claude Code. I reviewed and tested it myself.