Repository navigation
Don't append an empty zstd frame when a frame fills the output exactly - #8644
Conversation
|
This pull request's reviews are posted as pull request reviews: see the review of |
|
clanker review |
There was a problem hiding this comment.
⚠️ Out of date: this review is of277bdc2a; the pull request is now at264fc346. Someone with write access can commentclanky review(orclanker reviewor@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_andlastResultkeep 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.
277bdc2 to
e480393
Compare
when a zstd frame ends exactly at the end of the output buffer,
zstdCompressSync(),zstdCompress()andZstdCompressstreams 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 callZSTD_compressStream2()again in case more output is pending, but the frame has already ended, so thatZSTD_e_endcall starts a new one and ends it at once. The one-shot functions hit it at 40960, 81920, 163840... bytes and streams at multiples ofchunkSize: 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 framesthe 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