pub struct Compressor {
pub enabled: bool,
pub threshold_bytes: usize,
pub compression_type: CompressionType,
}Expand description
Compression configuration and utilities
Default Configuration:
- Enabled: true
- Threshold: 4KB (4096 bytes)
- Type: Snappy
Implementation Notes:
- C++ client: 4KB default (recommended)
- C++ server: 1KB default (too aggressive, causes asymmetry)
- Java: 8KB default (too conservative, less network savings)
- Rust: 4KB default (standardized best practice)
Configuration via config file:
[zusnet]
COMPRESS=true # Enable/disable compression
COMPRESS_THRESHSIZE=4096 # Threshold in bytes (default: 4096)Fields§
§enabled: boolCompression enabled
threshold_bytes: usizeThreshold size in bytes (only compress if data >= this size)
Recommended: 4096 (4KB)
- Balances compression benefit vs CPU cost
- Standardized across all implementations
compression_type: CompressionTypeCompression type
Current: Snappy (fast, good compression) Future: QuickLZ support for backward compatibility
Implementations§
Source§impl Compressor
impl Compressor
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new compressor with default settings
Compression Defaults:
- Threshold: 4KB (4096 bytes) - Recommended standardized value
- Type: QuickLZ (default in C++ and Java)
- Enabled: true
Rationale for 4KB threshold:
- C++ client default: 4KB
- C++ server default: 1KB (too aggressive)
- Java default: 8KB (too conservative)
- Production recommendation: 4KB balances CPU vs network savings
- Messages < 4KB compress poorly (low ratio, high CPU cost)
- Messages >= 4KB show 60-70% compression ratio
Rationale for QuickLZ as default:
- Matches C++ and Java default algorithm
- Better compression ratio than Snappy (~70% vs ~60%)
- Slightly slower than Snappy but more compatible
- Auto-detection works with existing C++/Java services
See: /Users/junfeiwang/Documents/zus-analysis/compression_defaults_comparison.md
Sourcepub fn with_config(
enabled: bool,
threshold_bytes: usize,
compression_type: CompressionType,
) -> Self
pub fn with_config( enabled: bool, threshold_bytes: usize, compression_type: CompressionType, ) -> Self
Create a compressor with custom settings
Sourcepub fn compress(&self, data: &[u8]) -> Result<(Bytes, bool)>
pub fn compress(&self, data: &[u8]) -> Result<(Bytes, bool)>
Compress data if it meets the threshold
Returns (compressed_data, was_compressed)
Sourcepub fn decompress(&self, data: &[u8]) -> Result<Bytes>
pub fn decompress(&self, data: &[u8]) -> Result<Bytes>
Decompress data
Auto-detects compression type by checking prefixes:
- “Snappy\0” (7 bytes) → Snappy format
- “qlz” (3 bytes) → QuickLZ format
- Otherwise → Try QuickLZ, fallback to uncompressed if it fails
This matches the C++ and Java auto-detection logic.
Trait Implementations§
Source§impl Clone for Compressor
impl Clone for Compressor
Source§fn clone(&self) -> Compressor
fn clone(&self) -> Compressor
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more