Skip to content

feat: add zstd and lz4 per-token compression codecs - #3028

Merged
oferchen merged 2 commits into
masterfrom
feature/zstd-lz4-per-token-codec
Mar 28, 2026
Merged

oferchen merged 2 commits into
masterfrom
feature/zstd-lz4-per-token-codec

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Extract zlib codec into dedicated zlib_codec.rs module with pub(super) visibility for strategy pattern dispatch
  • Add ZstdTokenEncoder/ZstdTokenDecoder (feature-gated on zstd) implementing upstream's CPRES_ZSTD mode with persistent ZSTD_CCtx/ZSTD_DCtx and ZSTD_e_flush at token boundaries
  • Add Lz4TokenEncoder/Lz4TokenDecoder (feature-gated on lz4) implementing upstream's CPRES_LZ4 mode with stateless per-chunk LZ4_compress_default/LZ4_decompress_safe
  • Wire both into the algorithm-agnostic CompressedTokenEncoder/CompressedTokenDecoder via enum dispatch with new_zstd()/new_lz4() constructors
  • All three codecs share the same wire framing (DEFLATED_DATA headers, token run encoding)

Test plan

  • CI passes (fmt, clippy, nextest) on all platforms
  • Zstd codec roundtrip tests pass (literal, block matches, large literal, see_token noop, consecutive blocks)
  • LZ4 codec roundtrip tests pass (same coverage as zstd)
  • Existing zlib compressed token tests remain green
  • Builds cleanly with --all-features, --features zstd, --features lz4, and default features (zlib only)

…atch

Split the monolithic CompressedTokenEncoder/Decoder into a layered
architecture: ZlibTokenEncoder/Decoder in zlib_codec.rs contain the
zlib-specific compression logic, while encoder.rs/decoder.rs become
thin enum wrappers that dispatch to the active codec.

This prepares for adding zstd and lz4 per-token codecs as additional
enum variants. The public API is unchanged - all existing call sites
continue to work without modification.

upstream: token.c uses a similar dispatch pattern with
CPRES_ZLIB/CPRES_ZLIBX/CPRES_ZSTD/CPRES_LZ4 enum.
Add ZstdTokenEncoder/ZstdTokenDecoder (feature-gated on `zstd`) and
Lz4TokenEncoder/Lz4TokenDecoder (feature-gated on `lz4`) implementing
the per-token compression wire format matching upstream rsync's
CPRES_ZSTD and CPRES_LZ4 modes.

Key differences from zlib:
- Zstd: persistent ZSTD_CCtx/DCtx, ZSTD_e_flush at boundaries, no
  sync marker stripping, see_token is noop
- LZ4: stateless per-chunk compression via LZ4_compress_default,
  retry with halved input if output exceeds MAX_DATA_COUNT, see_token
  is noop

Both codecs share the same wire framing (DEFLATED_DATA headers, token
run encoding) as zlib. The algorithm-agnostic encoder/decoder dispatch
via enum variants with new_zstd()/new_lz4() constructors.

upstream: token.c:send_zstd_token(), recv_zstd_token(),
send_compressed_token(SUPPORT_LZ4), recv_compressed_token(SUPPORT_LZ4)
@github-actions github-actions Bot added the enhancement New feature or request label Mar 28, 2026
@oferchen
oferchen merged commit a9ce04b into master Mar 28, 2026
34 of 35 checks passed
@oferchen
oferchen deleted the feature/zstd-lz4-per-token-codec branch March 28, 2026 07:49
oferchen added a commit that referenced this pull request May 1, 2026
* refactor: extract zlib codec into dedicated module for algorithm dispatch

Split the monolithic CompressedTokenEncoder/Decoder into a layered
architecture: ZlibTokenEncoder/Decoder in zlib_codec.rs contain the
zlib-specific compression logic, while encoder.rs/decoder.rs become
thin enum wrappers that dispatch to the active codec.

This prepares for adding zstd and lz4 per-token codecs as additional
enum variants. The public API is unchanged - all existing call sites
continue to work without modification.

upstream: token.c uses a similar dispatch pattern with
CPRES_ZLIB/CPRES_ZLIBX/CPRES_ZSTD/CPRES_LZ4 enum.

* feat: add zstd and lz4 per-token compression codecs

Add ZstdTokenEncoder/ZstdTokenDecoder (feature-gated on `zstd`) and
Lz4TokenEncoder/Lz4TokenDecoder (feature-gated on `lz4`) implementing
the per-token compression wire format matching upstream rsync's
CPRES_ZSTD and CPRES_LZ4 modes.

Key differences from zlib:
- Zstd: persistent ZSTD_CCtx/DCtx, ZSTD_e_flush at boundaries, no
  sync marker stripping, see_token is noop
- LZ4: stateless per-chunk compression via LZ4_compress_default,
  retry with halved input if output exceeds MAX_DATA_COUNT, see_token
  is noop

Both codecs share the same wire framing (DEFLATED_DATA headers, token
run encoding) as zlib. The algorithm-agnostic encoder/decoder dispatch
via enum variants with new_zstd()/new_lz4() constructors.

upstream: token.c:send_zstd_token(), recv_zstd_token(),
send_compressed_token(SUPPORT_LZ4), recv_compressed_token(SUPPORT_LZ4)
oferchen added a commit that referenced this pull request May 5, 2026
* refactor: extract zlib codec into dedicated module for algorithm dispatch

Split the monolithic CompressedTokenEncoder/Decoder into a layered
architecture: ZlibTokenEncoder/Decoder in zlib_codec.rs contain the
zlib-specific compression logic, while encoder.rs/decoder.rs become
thin enum wrappers that dispatch to the active codec.

This prepares for adding zstd and lz4 per-token codecs as additional
enum variants. The public API is unchanged - all existing call sites
continue to work without modification.

upstream: token.c uses a similar dispatch pattern with
CPRES_ZLIB/CPRES_ZLIBX/CPRES_ZSTD/CPRES_LZ4 enum.

* feat: add zstd and lz4 per-token compression codecs

Add ZstdTokenEncoder/ZstdTokenDecoder (feature-gated on `zstd`) and
Lz4TokenEncoder/Lz4TokenDecoder (feature-gated on `lz4`) implementing
the per-token compression wire format matching upstream rsync's
CPRES_ZSTD and CPRES_LZ4 modes.

Key differences from zlib:
- Zstd: persistent ZSTD_CCtx/DCtx, ZSTD_e_flush at boundaries, no
  sync marker stripping, see_token is noop
- LZ4: stateless per-chunk compression via LZ4_compress_default,
  retry with halved input if output exceeds MAX_DATA_COUNT, see_token
  is noop

Both codecs share the same wire framing (DEFLATED_DATA headers, token
run encoding) as zlib. The algorithm-agnostic encoder/decoder dispatch
via enum variants with new_zstd()/new_lz4() constructors.

upstream: token.c:send_zstd_token(), recv_zstd_token(),
send_compressed_token(SUPPORT_LZ4), recv_compressed_token(SUPPORT_LZ4)
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