Skip to content

Don't append an empty zstd frame when a frame fills the output exactly - #8644

Merged
bartlomieju merged 2 commits into
cloudflare:mainfrom
maschwenk:zstd-encoder-exact-fill
Nov 27, 2026
Merged

bartlomieju merged 2 commits into
cloudflare:mainfrom
maschwenk:zstd-encoder-exact-fill

Conversation

@maschwenk

Copy link
Copy Markdown
Contributor

when a zstd frame ends exactly at the end of the output buffer, zstdCompressSync(), zstdCompress() and ZstdCompress streams append a second, empty frame (13 bytes with a checksum, 9 without). On a full buffer the sync loop and the stream's JS loop call ZSTD_compressStream2() again in case more output is pending, but the frame has already ended, so that ZSTD_e_end call starts a new one and ends it at once. The one-shot functions hit it at 40960, 81920, 163840... bytes and streams at multiples of chunkSize: compressing 40946 random bytes at level 1 with a checksum should give one 40960-byte frame but returns 40973 bytes. It still decodes, just as two frames

the encoder now skips that call when the previous one ended a frame exactly at the end of the buffer, as Node's decoder does when decompressing. Node appends the same empty frame at multiples of 16384 bytes, so at those sizes workerd's output is now 13 bytes shorter than Node's, and at 40960 the two match. The new test checks frames ending at 16, 32 and 40 KiB through the sync, info, callback and stream APIs and fails without the fix

@maschwenk
maschwenk requested review from a team as code owners November 19, 2026 03:44
@clanky-the-clanker

clanky-the-clanker Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

This pull request's reviews are posted as pull request reviews: see the review of 277bdc2a.

@jasnell

jasnell commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

clanker review

@clanky-the-clanker clanky-the-clanker Bot 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.

⚠️ Out of date: this review is of 277bdc2a; the pull request is now at 264fc346. Someone with write access can comment clanky review (or clanker review or @clanky-the-clanker review) to review the new commits.

✅ Approved — The fix is narrowly guarded, the flag is reset correctly, and the new test covers every affected API path. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

The ZstdEncoderContext::work() guard is correct for both callers: the sync syncProcessBuffer loop and the JavaScript processCallback/processChunkSync loops.

  • When it skips: only after a call that returned 0 with ZSTD_e_end, filled the output exactly, and has no input left. No pending input or output can be lost.
  • Error state: error_ and lastResult keep their earlier, non-error values.
  • Stale flag: resetStream() clears the flag, so a later frame cannot inherit it.
  • Test: the size arithmetic matches the zstd frame layout. The test covers the sync, info, callback and stream paths.

📝 Documentation: ✅ LGTM

The new comments describe the current behaviour and follow the repository's comment guidelines.


Reviewed commit: 277bdc2a

When the frame from zstdCompressSync(), zstdCompress() or a ZstdCompress
stream ended exactly at the end of an output buffer, the output carried
a second, empty frame (13 bytes with a checksum, 9 without) after it.
A full output buffer makes the sync loop, and the stream's JavaScript
loop, call ZSTD_compressStream2() again with no new input in case more
output is pending. The frame has ended by then, so with ZSTD_e_end that
call begins a new frame and ends it at once.

The one-shot functions hit this when the frame was 40960, 81920,
163840, ... bytes long, and streams when it was a multiple of
chunkSize (16384 by default). The output still decoded to the input,
but as two frames, and its size was off by up to 13 bytes.

The encoder now skips that call when the previous one ended a frame
exactly at the end of the output buffer, the way the decoder already
ignores the same call after a frame. A ZSTD_e_end flush that follows
anything else still writes an empty frame, as Node.js does.

Node.js has the same artifact on its stream path, which its sync
functions use too, at multiples of 16384 bytes; workerd's output is a
single frame at every size, so at those sizes it is 13 bytes shorter
than Node's.

zstdCompressFrameEndsAtOutputBufferEndTest builds frames that end on
both buffer boundaries and checks their size through the sync, info,
callback and stream APIs. It fails without this change.
@maschwenk
maschwenk force-pushed the zstd-encoder-exact-fill branch from 277bdc2 to e480393 Compare November 21, 2026 01:52
@bartlomieju
bartlomieju merged commit 54fd5e3 into cloudflare:main Nov 27, 2026
51 of 60 checks passed
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