pub struct StorageCompressionRegistry {
pub profiles: HashMap<CompressionCodec, CodecProfile>,
}Expand description
Registry that tracks compression codec profiles and recommends a codec based on observed performance data and data characteristics.
§Example
use ipfrs_storage::compression_registry::{
StorageCompressionRegistry, CompressionCodec, DataCharacteristics,
};
let mut registry = StorageCompressionRegistry::new();
registry.record_usage(CompressionCodec::Zstd, 1024, 400, 480, 90);
let chars = DataCharacteristics {
size_bytes: 4096,
is_text: false,
is_already_compressed: false,
latency_sensitive: false,
};
let rec = registry.recommend(&chars);
println!("Recommended: {:?} — {}", rec.codec, rec.reason);Fields§
§profiles: HashMap<CompressionCodec, CodecProfile>Per-codec runtime profiles.
Implementations§
Source§impl StorageCompressionRegistry
impl StorageCompressionRegistry
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new registry pre-populated with default profiles for all five codecs.
Default values are based on typical real-world measurements:
| Codec | avg_ratio | compress µs | decompress µs |
|---|---|---|---|
| Zstd | 0.40 | 500 | 100 |
| Lz4 | 0.60 | 50 | 30 |
| Snappy | 0.70 | 100 | 50 |
| Brotli | 0.35 | 2000 | 100 |
| None | 1.00 | 1 | 1 |
Sourcepub fn record_usage(
&mut self,
codec: CompressionCodec,
original_bytes: u64,
compressed_bytes: u64,
compress_micros: u64,
decompress_micros: u64,
)
pub fn record_usage( &mut self, codec: CompressionCodec, original_bytes: u64, compressed_bytes: u64, compress_micros: u64, decompress_micros: u64, )
Record a compression event and update the codec profile.
For the first use (uses == 0 before the call) the measurements are
stored directly. For subsequent uses an exponential moving average
(α = 0.1) is applied:
new_value = 0.9 * old_value + 0.1 * sample§Arguments
codec— the codec that was used.original_bytes— uncompressed size.compressed_bytes— size after compression.compress_micros— time taken to compress.decompress_micros— time taken to decompress.
Sourcepub fn recommend(&self, data: &DataCharacteristics) -> CodecRecommendation
pub fn recommend(&self, data: &DataCharacteristics) -> CodecRecommendation
Recommend a compression codec for the given data characteristics.
Decision logic (in order of priority):
- Already compressed →
None(“already compressed”). - Very small block (< 256 B) →
None(“too small”). - Latency-sensitive →
Lz4(“latency optimized”). - Text data →
Brotli(“text compression”). - Otherwise → codec with the best
efficiency_score(excludingNone) (“best efficiency”).
Sourcepub fn get_profile(&self, codec: CompressionCodec) -> Option<&CodecProfile>
pub fn get_profile(&self, codec: CompressionCodec) -> Option<&CodecProfile>
Return a reference to the profile for the given codec, if present.
Sourcepub fn stats(&self) -> CompressionRegistryStats
pub fn stats(&self) -> CompressionRegistryStats
Compute aggregate statistics across all registered profiles.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for StorageCompressionRegistry
impl RefUnwindSafe for StorageCompressionRegistry
impl Send for StorageCompressionRegistry
impl Sync for StorageCompressionRegistry
impl Unpin for StorageCompressionRegistry
impl UnsafeUnpin for StorageCompressionRegistry
impl UnwindSafe for StorageCompressionRegistry
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more