Skip to content

feat: add AdaptiveLevelStrategy for runtime compression level tuning#3391

Merged
oferchen merged 1 commit intomasterfrom
feat/adaptive-compression-level
Apr 26, 2026
Merged

feat: add AdaptiveLevelStrategy for runtime compression level tuning#3391
oferchen merged 1 commit intomasterfrom
feat/adaptive-compression-level

Conversation

@oferchen
Copy link
Copy Markdown
Owner

Summary

  • Adds AdaptiveLevelStrategy trait + DefaultAdaptiveLevelStrategy in crates/compress/src/strategy/adaptive_level.rs for sender-side, per-block compression level adaptation.
  • The default impl uses an EWMA of the observed compression ratio with configurable thresholds (poor_ratio_threshold ratchets level down to save CPU, good_ratio_threshold ratchets level up for better ratio) and clamps to a codec-appropriate LevelBounds (zlib 1..=9, zstd 1..=19).
  • Wire-compatible: only the sender's encoder level changes; token framing, message headers, and codec advertisement are untouched. Never advertised as a protocol capability.
  • Opt-in: the existing fixed-level encoder path stays the default. This trait is wired in only when an explicit configuration knob requests adaptive tuning.
  • Re-exported from compress::strategy and follows the existing Strategy/Profile module patterns.

Test plan

  • level_bounds_orders_arguments and level_bounds_clamps_within_range cover bounds construction.
  • first_observation_seeds_ewma covers EWMA initialisation.
  • monotonic_bad_ratio_decreases_level drives 50 incompressible blocks and asserts the level lands at bounds.min.
  • monotonic_good_ratio_increases_level drives 50 highly compressible blocks and asserts the level lands at bounds.max.
  • neutral_ratio_holds_level confirms ratios between thresholds keep the level steady.
  • ewma_smooths_outlier confirms a single bad block after good ones moves the smoothed value but does not jump straight to it.
  • reset_clears_history, rejects_non_finite_observation, for_zstd_uses_wider_bounds, alpha_is_clamped_to_unit_interval cover edge cases.
  • cargo fmt --all clean.
  • CI: fmt+clippy, nextest (stable), Windows, macOS, Linux musl must pass.

Introduces a sender-side strategy that picks per-block compression levels
from an EWMA of observed compression ratios so highly compressible
payloads get more CPU and incompressible payloads get less. Default
implementation is opt-in and wire-compatible: only the encoder level
changes, not the protocol or token framing.
@oferchen oferchen force-pushed the feat/adaptive-compression-level branch from bfacf36 to c3f7e22 Compare April 26, 2026 09:54
@oferchen oferchen merged commit 6aac9c2 into master Apr 26, 2026
40 of 41 checks passed
@oferchen oferchen deleted the feat/adaptive-compression-level branch April 26, 2026 12:56
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