Skip to content

feat: implement per-token lz4 frame flush - #2977

Merged
oferchen merged 1 commit into
masterfrom
feat/lz4-per-token-flush
Mar 22, 2026
Merged

oferchen merged 1 commit into
masterfrom
feat/lz4-per-token-flush

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Add flush() method to CountingLz4Encoder that delegates to FrameEncoder::flush(), emitting buffered data as a complete LZ4 block so the receiver can decompress incrementally without waiting for stream finalization
  • Add flush() method to CountingZstdEncoder that delegates to ZstdEncoder::flush(), providing the same per-token flush semantics for zstd compression
  • Both methods are required by CompressedWriter::flush_compressed() in the transfer crate, which calls encoder.flush() at token boundaries mirroring upstream rsync's token.c:send_deflated_token() pattern

Context

Like zlib's Z_SYNC_FLUSH, lz4 and zstd compression need per-token flush to allow incremental decompression. Without flush methods on the counting encoder wrappers, the CompressedWriter cannot materialize buffered compressed data at token boundaries, preventing the receiver from decompressing tokens as they arrive.

The zlib CountingZlibEncoder already had this via its Write trait impl (where flush() triggers Z_SYNC_FLUSH). The lz4 and zstd encoders were missing this capability.

Test plan

  • CI passes fmt+clippy, nextest on all platforms
  • New tests verify flush emits compressed data to the sink
  • New tests verify flush-then-finish produces valid decompressible streams
  • New tests verify multiple flush cycles with varying token sizes round-trip correctly
  • New tests verify empty flush is a safe no-op
  • New tests verify bytes-written counter increases after flush

Add flush() methods to CountingLz4Encoder and CountingZstdEncoder that
emit all buffered data as decompressible output, matching zlib's
Z_SYNC_FLUSH semantics. This enables the receiver to decompress tokens
incrementally without waiting for stream finalization.

The LZ4 flush delegates to FrameEncoder::flush() which writes any
buffered uncompressed data as a complete LZ4 block. The zstd flush
delegates to ZstdEncoder::flush() which triggers a zstd flush operation.

Both methods are called by CompressedWriter::flush_compressed() at token
boundaries, mirroring upstream rsync's per-token flush pattern in
token.c:send_deflated_token().

Includes comprehensive tests verifying flush emits compressed data,
flush-then-finish produces valid streams, multiple flush cycles
round-trip correctly, and empty flush is a safe no-op.
@github-actions github-actions Bot added the enhancement New feature or request label Mar 22, 2026
@oferchen
oferchen merged commit df8dea3 into master Mar 22, 2026
12 of 17 checks passed
@oferchen
oferchen deleted the feat/lz4-per-token-flush branch March 22, 2026 14:10
oferchen added a commit that referenced this pull request May 1, 2026
Add flush() methods to CountingLz4Encoder and CountingZstdEncoder that
emit all buffered data as decompressible output, matching zlib's
Z_SYNC_FLUSH semantics. This enables the receiver to decompress tokens
incrementally without waiting for stream finalization.

The LZ4 flush delegates to FrameEncoder::flush() which writes any
buffered uncompressed data as a complete LZ4 block. The zstd flush
delegates to ZstdEncoder::flush() which triggers a zstd flush operation.

Both methods are called by CompressedWriter::flush_compressed() at token
boundaries, mirroring upstream rsync's per-token flush pattern in
token.c:send_deflated_token().

Includes comprehensive tests verifying flush emits compressed data,
flush-then-finish produces valid streams, multiple flush cycles
round-trip correctly, and empty flush is a safe no-op.
oferchen added a commit that referenced this pull request May 5, 2026
Add flush() methods to CountingLz4Encoder and CountingZstdEncoder that
emit all buffered data as decompressible output, matching zlib's
Z_SYNC_FLUSH semantics. This enables the receiver to decompress tokens
incrementally without waiting for stream finalization.

The LZ4 flush delegates to FrameEncoder::flush() which writes any
buffered uncompressed data as a complete LZ4 block. The zstd flush
delegates to ZstdEncoder::flush() which triggers a zstd flush operation.

Both methods are called by CompressedWriter::flush_compressed() at token
boundaries, mirroring upstream rsync's per-token flush pattern in
token.c:send_deflated_token().

Includes comprehensive tests verifying flush emits compressed data,
flush-then-finish produces valid streams, multiple flush cycles
round-trip correctly, and empty flush is a safe no-op.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant