Skip to content

ws: get random masks in batches - #23595

Closed
mvanslobbe wants to merge 2 commits into
curl:masterfrom
mvanslobbe:perf/ws-mask-pool
Closed

mvanslobbe wants to merge 2 commits into
curl:masterfrom
mvanslobbe:perf/ws-mask-pool

Conversation

@mvanslobbe

Copy link
Copy Markdown
Contributor

Every WebSocket frame libcurl sends needs a new 4-byte mask. Each mask was fetched with its own Curl_rand() call, and Curl_rand_bytes() asks the TLS library for only four bytes per call, so sending small frames spent most of its CPU time in RAND_bytes().

The cost is per call, not per byte. Profiling ./perf wssend with 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() and strcmp()), rwlock lock and unlock, re-keying the CTR DRBG after each request, and a RAND_status() check that ossl_random() makes on every call here.

  • Curl_rand_bytes() fills the whole buffer with one Curl_ssl_random() call, unless CURL_ENTROPY forces the bytes in a debug build
  • the debug assert that Curl_ssl_random() only gets four bytes is dropped; every backend's random function takes any length
  • the websocket encoder keeps a 256-byte pool of random bytes and takes each frame's mask from it, refilling it every 64 frames

This also adds wssend, a perf test that upgrades a connection to WebSocket over a local socket pair, with no server involved, and sends frames with curl_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)

runner compiler 16 bytes 125 bytes 1024 bytes 16384 bytes
x86-64 (Xeon 6973P-C) gcc 13 1648 → 961 (−42%) 1661 → 976 (−41%) 1764 → 1063 (−40%) 3681 → 2906 (−21%)
x86-64 (EPYC 7763) clang 18 3982 → 2607 (−35%) 4018 → 2654 (−34%) 4193 → 2821 (−33%) 7580 → 6180 (−19%)
arm64 (Neoverse-N2) gcc 13 2547 → 1407 (−45%) 2573 → 1431 (−44%) 2767 → 1614 (−42%) 5650 → 4393 (−22%)
arm64 (Neoverse-N2) clang 18 2525 → 1394 (−45%) 2538 → 1411 (−44%) 2701 → 1568 (−42%) 5164 → 3926 (−24%)
macOS arm64 (M1) Apple clang 21 1427 → 922 (−35%) 1635 → 1065 (−35%) 1884 → 1236 (−34%) 5916 → 6116 (+3%)

Locally, i7-12700H, Linux, OpenSSL 3, pinned to one core:

compiler 16 bytes 125 bytes 1024 bytes 16384 bytes
gcc 15 1529 → 789 (−48%) 1544 → 807 (−48%) 1646 → 899 (−45%) 3549 → 2741 (−23%)
clang 22 1501 → 772 (−49%) 1511 → 786 (−48%) 1576 → 839 (−47%) 3143 → 2342 (−26%)

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 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

bagder commented Oct 26, 2026

Copy link
Copy Markdown
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

Copy link
Copy Markdown
Contributor Author

@bagder Rebased onto master and force-pushed. The conflict was only the perf test list in tests/perf/Makefile.inc and tests/perf/README.md after wsrecv landed; both tests are now listed side by side.

@bagder bagder closed this in de3969f Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants