Repository navigation
Allocate stream block buffers only as block data arrives - #167
Merged
Merged
Conversation
LZ4BlockInputStream sized its output buffer from the originalLen and its compressed buffer from the compressedLen of a block header before reading any of the block data. A 21 byte header could therefore make it allocate about 64 MiB, which stays referenced while the underlying stream stalls. LZ4FrameInputStream did the same with two buffers of the frame's maximum block size, up to 8 MiB, for a block header of any size. Read block data into buffers that only grow as the data arrives, to at most twice the data read so far, and grow them geometrically up to the maximum block size, so that a sequence of slightly larger blocks does not reallocate every time. Allocate the output buffer of a compressed block only once the compressed data has been read, and size it for the maximum LZ4 compression ratio of 255, which LZ4BlockInputStream now also checks in the block header. The memory a peer can make a stream hold is now proportional to the data it actually sent. The block stream's output buffer growth is also capped at the maximum block size now. A truncated block in LZ4FrameInputStream is still reported as a plain IOException, not as an EOFException that callers may take for a clean end of data. An Error such as OutOfMemoryError while reading a block now also makes the stream fail, since the block was already partially consumed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
LZ4BlockInputStreamsized its output buffer from theoriginalLenand its compressed buffer from thecompressedLenof a block header before reading any of the block data. A 21 byte header could therefore make it allocate about 64 MiB, which stays referenced while the underlying stream stalls (e.g. a peer that sends a header and then nothing).LZ4FrameInputStreamdid the same with two buffers of the frame's maximum block size (up to 8 MiB) for a block header of any size. The allocations are bounded by the maximum block size of the formats, but not by the amount of data actually received.Changes
LZ4Utils.readFullyGrowingreads block data into a buffer that only grows as the data arrives, to at most twice the data read so far (at leastmin(len, 64 KiB)). Beyond the requested length, it grows geometrically up to the maximum block size, so that a sequence of slightly larger blocks (e.g. from flushing growing messages) does not reallocate every time.MAX_COMPRESSION_RATIO, moved fromLZ4DecompressorWithLengthtoLZ4Constants).LZ4BlockInputStreamnow rejects block headers whose original length exceeds that ratio; valid LZ4 data cannot exceed it.LZ4BlockInputStreamis capped at the block's maximum size (it could previously reach 48 MiB).LZ4FrameInputStreamis still reported as a plainIOException("Stream ended prematurely"), not as anEOFExceptionthat callers may take for a clean end of data.Errorsuch asOutOfMemoryErrorwhile reading a block now also makes both streams fail permanently, since the block was already partially consumed.acceptOversizedBlocksjavadoc no longer warns about attacker-controlled allocations: oversized blocks can now only allocate what was actually read.With this, the memory a stream holds is proportional to the data it received (at most 255× for highly compressible blocks, which is inherent to decompression). Measured with a header-only input: 67 MB → 64 KiB for the block stream, 8 MiB → 64 KiB for the frame stream.
Tests
testBlockBuffersReusedAcrossFramesnow uses blocks that need the full 4 MiB output buffer; with the smaller buffers it could no longer detect missing reuse.🤖 Generated with Claude Code