Repository navigation
Speed up small frame flushes and always close the underlying stream - #199
Open
Dominik-O22 wants to merge 2 commits into
Open
Dominik-O22 wants to merge 2 commits into
Dominik-O22 wants to merge 2 commits into
Conversation
LZ4FrameOutputStream cleared its whole compressed buffer before every block, over 4 MB with the default block size, however little data was in the block. Only the bytes that the compressor returned are written and checksummed, so this has no effect on the output. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If close() could not write the remaining data, LZ4FrameOutputStream and LZ4BlockOutputStream left the underlying stream open, and another close() wrote the data again. Now the underlying stream is always closed, once. LZ4FrameOutputStream also wrote knownSize to the header without checking it. The resulting frame could not be decoded. close() now throws instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
LZ4FrameOutputStreamclears its whole compressed buffer before every block, over 4 MB with the default block size. A 100-byte write and flush therefore takes about 31 µs, almost all of it spent clearing. While looking at that class I found two reliability problems:close()can't write the remaining data, e.g. because the disk is full,LZ4FrameOutputStreamandLZ4BlockOutputStreamleave the underlying stream open. A secondclose()writes the data again.LZ4FrameOutputStreamwritesknownSizeto the header without checking it. A wrong size gives a frame that neitherLZ4FrameInputStreamnor thelz4CLI accepts, and the writer gets no error.Change
close()always closes the underlying stream, once, and throws the original exception.GZIPOutputStreamcloses the underlying stream in this case too.knownSize: ifCONTENT_SIZEis set andknownSizeis greater than 0,close()throws anIOExceptionwhen a different number of bytes was written, and leaves the frame unterminated. 0 stays unchecked, as in liblz4, because thelz4CLI decodes those frames.The two commits are independent if you prefer separate PRs.
Behavior to be aware of
knownSizenow gets an exception on write instead of a failure on read.Verification
mainfor 432 frames (96 MB): safe, Unsafe and native, fast and HC, all block sizes, random flushes, both checksums../mvnw verifypasses. The tests inLZ4OutputStreamTestfail on 12 of 12 hand-made mutations of the new code.close()calls checked on JDK 7, 17 and 25, sinceFilterOutputStream.close()differs between them.Created with Claude Opus 5.5 in Claude Code.