Skip to content

Speed up small frame flushes and always close the underlying stream - #199

Open
Dominik-O22 wants to merge 2 commits into
yawkat:mainfrom
Dominik-O22:frame-output-stream
Open

Dominik-O22 wants to merge 2 commits into
yawkat:mainfrom
Dominik-O22:frame-output-stream

Conversation

@Dominik-O22

Copy link
Copy Markdown

LZ4FrameOutputStream clears 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:

  • If close() can't write the remaining data, e.g. because the disk is full, LZ4FrameOutputStream and LZ4BlockOutputStream leave the underlying stream open. A second close() writes the data again.
  • LZ4FrameOutputStream writes knownSize to the header without checking it. A wrong size gives a frame that neither LZ4FrameInputStream nor the lz4 CLI accepts, and the writer gets no error.

Change

  • No more clearing. Only the bytes that the compressor returned are written and checksummed, so the zeros never reached the output.
  • close() always closes the underlying stream, once, and throws the original exception. GZIPOutputStream closes the underlying stream in this case too.
  • knownSize: if CONTENT_SIZE is set and knownSize is greater than 0, close() throws an IOException when a different number of bytes was written, and leaves the frame unterminated. 0 stays unchecked, as in liblz4, because the lz4 CLI decodes those frames.

The two commits are independent if you prefer separate PRs.

Behavior to be aware of

  • Code that declares a wrong knownSize now gets an exception on write instead of a failure on read.
  • A compressor that returns more bytes than it wrote would now emit leftover compressed data from the same stream instead of zeros. The frame is corrupt either way.

Verification

  • 100-byte write and flush to a file, default 4 MB blocks: 31.9 → 0.53 µs (median of 5 alternating runs, JDK 25, Ryzen 7 9850X3D). A 10 KB flush goes from 40 to 8.7 µs. Writing 32 MB without flushes is unchanged at 47 ms.
  • Output is byte-identical to main for 432 frames (96 MB): safe, Unsafe and native, fast and HC, all block sizes, random flushes, both checksums.
  • ./mvnw verify passes. The tests in LZ4OutputStreamTest fail on 12 of 12 hand-made mutations of the new code.
  • Failing and repeated close() calls checked on JDK 7, 17 and 25, since FilterOutputStream.close() differs between them.

Created with Claude Opus 5.5 in Claude Code.

Dominik-O22 and others added 2 commits November 29, 2026 20:17
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

No deployments
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