Skip to content

perf(websocket): decompress each permessage-deflate frame with one zlib write - #6192

Closed
deadstrobe5 wants to merge 1 commit into
nodejs:mainfrom
deadstrobe5:perf/ws-inflate-one-write
Closed

deadstrobe5 wants to merge 1 commit into
nodejs:mainfrom
deadstrobe5:perf/ws-inflate-one-write

Conversation

@deadstrobe5

Copy link
Copy Markdown

Rationale

PerMessageDeflate.decompress() made three calls into the inflate stream per frame: write(payload), write(tail) and flush(). Each one is a separate trip to the libuv thread pool, and the receiver waits for the last one before it parses the next frame, so a small compressed message cost about 55 us of CPU and 150 us of wall time on the client.

The inflate stream is now created with flush: Z_SYNC_FLUSH, and each frame is a single write(fin ? Buffer.concat([payload, tail]) : payload, cb). Because every write is a sync flush, all the output for the frame has been emitted by the time the write callback runs, the same guarantee the empty flush write gave. The data listener, the maxPayloadSize path and the error handler are unchanged, and the callback returns early on an error or once the inflater has been dropped.

Sequential decompress() calls on one context, Node v24.22.2, median of 7 alternating rounds:

message size before (cpu / wall per msg) after (cpu / wall per msg)
60 B 57.6 us / 161 us 20.3 us / 61.6 us
1 KiB 53.6 us / 150 us 20.3 us / 51.7 us
64 KiB 228 us / 543 us 186 us / 490 us

A new test sends messages that share the deflate context, including a fragmented one whose first fragment holds no complete symbol, and checks that they arrive intact and in order. The WebSocket and permessage-deflate tests pass.

Status

…ib write

Signed-off-by: deadstrobe5 <afonso.diamantino2@gmail.com>
@tsctx

tsctx commented Dec 1, 2026

Copy link
Copy Markdown
Member

I don't want to copy the entire data just to append a 4-byte TRAILER.

@tsctx tsctx closed this Dec 1, 2026
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.

2 participants