Skip to content

perf(sio): compress each client file only once - #5603

Open
b84330808 wants to merge 1 commit into
socketio:mainfrom
b84330808:perf-cache-compressed-client
Open

b84330808 wants to merge 1 commit into
socketio:mainfrom
b84330808:perf-cache-compressed-client

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behavior

With serveClient: true (the default), the Node.js HTTP server compresses the client bundle again for every request (<script src="/socket.io/socket.io.js">). Brotli runs at its default quality (11), which takes about 60 ms of CPU for socket.io.min.js and about 200 ms for socket.io.js, per request, in the libuv threadpool.

New behavior

The compressed content is computed on the first request for a given file and encoding, then kept in memory (a few hundred KB at most for all the files). The bytes sent are the same as before (same size, same sha256 for the br response). Uncompressed responses still stream from disk, and the uWebSockets.js path is untouched.

A failed read or compression is not cached, so the next request tries again.

Numbers: 100 (or 40, 200) requests, 4 in flight, client and server in the same Node.js process, base and patched alternated 5 times each, median shown. Windows 11, i7-9700, Node.js 24.15.0.

request base patched
socket.io.min.js, br 56 req/s, 6.6 s CPU per 100 req 744 req/s, 0.13 s CPU
socket.io.js, br 17 req/s, 9.2 s CPU per 40 req 153 req/s, 0.28 s CPU
socket.io.js, gzip 562 req/s, 1.1 s CPU per 200 req 1920 req/s, 0.17 s CPU
socket.io.js, no compression 772 req/s 774 req/s

The new test checks that two consecutive br responses both decode to the file in client-dist/.

Other information (e.g. related issues)

The benchmark script is just an HTTP client loop against new Server(httpServer); I can add it somewhere if useful.

The client bundle served by the Node.js HTTP server was compressed on
every request. With brotli at its default quality (11), that costs about
60 ms of CPU for socket.io.min.js and 200 ms for socket.io.js per
request, in the libuv threadpool.

The compressed content is now computed on the first request and kept in
memory, keyed by file and encoding. The response bytes are unchanged.
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.

1 participant