Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
358 changes: 136 additions & 222 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rust-version.workspace = true
debug = true

[workspace]
members = ["preflate", "container", "dll", "util", "fuzz"]
members = ["preflate", "container", "dll", "util"]
resolver = "2"

[workspace.package]
Expand All @@ -28,4 +28,4 @@ preflate-container = { path = "container" }
libz-sys = "1.1"
libdeflate-sys = "1.19"
libz-ng-sys = "1.1.12"
miniz_oxide = "0.8"
miniz_oxide = "0.9"
2 changes: 1 addition & 1 deletion container/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ lepton_jpeg = "0.5.1"

[dev-dependencies]
env_logger = "0.11"
miniz_oxide = "0.8"
miniz_oxide = "0.9"
adler32 = "1.2.0"

[lib]
Expand Down
40 changes: 33 additions & 7 deletions container/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
#![forbid(trivial_casts)]
#![forbid(trivial_numeric_casts)]
#![forbid(non_ascii_idents)]
#![forbid(unused_extern_crates)]
#![forbid(unused_import_braces)]
#![forbid(redundant_lifetimes)]
#![forbid(single_use_lifetimes)]
#![forbid(unused_crate_dependencies)]
#![forbid(unused_extern_crates)]
#![forbid(unused_lifetimes)]
#![forbid(unused_macro_rules)]
#![forbid(macro_use_extern_crate)]
#![forbid(missing_unsafe_on_extern)]
#![forbid(unused_import_braces)]
#![forbid(unused_lifetimes)]
#![warn(unused_extern_crates)]
#![warn(unused_macro_rules)]
#![warn(unused_crate_dependencies)]
#![warn(macro_use_extern_crate)]

mod container_common;
mod container_read;
Expand All @@ -34,6 +33,33 @@ pub use container_write::PreflateContainerProcessor;

pub use utils::process_limited_buffer;

/// Convenience wrapper: compresses an entire input stream into the preflate container format.
pub fn preflate_whole_into_container(
config: &PreflateContainerConfig,
input: &mut impl std::io::Read,
output: &mut impl std::io::Write,
) -> preflate_rs::Result<()> {
let mut processor = PreflateContainerProcessor::new(config, 9, false);
let mut buf = Vec::new();
input.read_to_end(&mut buf).map_err(|e| {
preflate_rs::PreflateError::new(preflate_rs::ExitCode::GeneralFailure, e.to_string())
})?;
processor.process_buffer(&buf, true, output)
}

/// Convenience wrapper: recreates the original data from a preflate container stream.
pub fn recreate_whole_from_container(
input: &mut impl std::io::Read,
output: &mut impl std::io::Write,
) -> preflate_rs::Result<()> {
let mut processor = RecreateContainerProcessor::new(128 * 1024 * 1024);
let mut buf = Vec::new();
input.read_to_end(&mut buf).map_err(|e| {
preflate_rs::PreflateError::new(preflate_rs::ExitCode::GeneralFailure, e.to_string())
})?;
processor.process_buffer(&buf, true, output)
}

#[cfg(test)]
static INIT: std::sync::Once = std::sync::Once::new();

Expand Down
2 changes: 0 additions & 2 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ name = "fuzz_target_1"
path = "fuzz_target_1.rs"
test = false
doc = false
required-features = ["fuzzing"]

[[bin]]
name = "fuzz_container"
path = "fuzz_container.rs"
test = false
doc = false
required-features = ["fuzzing"]

[features]
fuzzing = []
2 changes: 1 addition & 1 deletion package/Preflate.Rust.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>Preflate.Rust</id>
<version>0.7.5.0</version>
<version>0.7.6.0</version>
<title>PreflateRs Compression Rust binaries and libraries</title>
<authors>kristofr</authors>
<owners>kristofr</owners>
Expand Down
12 changes: 6 additions & 6 deletions preflate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
#![forbid(trivial_casts)]
#![forbid(trivial_numeric_casts)]
#![forbid(non_ascii_idents)]
#![forbid(unused_extern_crates)]
#![forbid(unused_import_braces)]
#![forbid(redundant_lifetimes)]
#![forbid(single_use_lifetimes)]
#![forbid(unused_crate_dependencies)]
#![forbid(unused_extern_crates)]
#![forbid(unused_lifetimes)]
#![forbid(unused_macro_rules)]
#![forbid(macro_use_extern_crate)]
#![forbid(missing_unsafe_on_extern)]
#![forbid(unused_import_braces)]
#![warn(unused_crate_dependencies)]
#![warn(unused_extern_crates)]
#![warn(unused_lifetimes)]
#![warn(unused_macro_rules)]

mod bit_helper;
mod cabac_codec;
Expand All @@ -43,6 +42,7 @@ pub use preflate_error::ExitCode;
pub use preflate_error::{AddContext, PreflateError, Result, err_exit_code};
pub use preflate_input::{PlainText, PreflateInput};

pub use statistical_codec::{CodecCorrection, CountNonDefaultActions};
pub use stream_processor::{
PreflateStreamChunkResult, PreflateStreamProcessor, RecreateStreamProcessor,
preflate_whole_deflate_stream, recreate_whole_deflate_stream,
Expand Down
110 changes: 78 additions & 32 deletions tests/end_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,12 @@ fn compression_benchmark_overhead_size() {
Ok(30),
Ok(36),
Ok(23),
Ok(25),
Ok(4070),
Ok(4474),
Ok(3759),
Ok(23),
Ok(44),
Ok(2503),
Ok(1803),
Ok(49),
Ok(37),
Ok(27),
Err(ExitCode::NoCompressionCandidates),
],
},
Expand Down Expand Up @@ -464,40 +464,86 @@ fn compression_benchmark_overhead_size() {
},
];

for i in bench.iter() {
let mut result = Vec::new();

for level in 0..=9 {
let compressed = (i.compress_fn)(&original, level);

let r = preflate_whole_deflate_stream(&compressed, &PreflateConfig::default());
struct LevelResult {
compressed_size: usize,
overhead: Result<usize, ExitCode>,
}

match r {
Ok((r, _)) => {
result.push(Ok(r.corrections.len()));
}
Err(e) => {
result.push(Err(e.exit_code()));
// Collect all results first so the table can be printed before asserting.
let all_results: Vec<Vec<LevelResult>> = bench
.iter()
.map(|i| {
(0..=9)
.map(|level| {
let compressed = (i.compress_fn)(&original, level);
let compressed_size = compressed.len();
let overhead = match preflate_whole_deflate_stream(
&compressed,
&PreflateConfig::default(),
) {
Ok((r, _)) => Ok(r.corrections.len()),
Err(e) => Err(e.exit_code()),
};
LevelResult {
compressed_size,
overhead,
}
})
.collect()
})
.collect();

// Print a comparison table so regressions and improvements are visible at a glance.
println!(
"\nOverhead benchmark — original = {} bytes\n",
original.len()
);
for (i, results) in bench.iter().zip(all_results.iter()) {
println!("{}:", i.name);
println!(
" {:>3} {:>11} {:>11} {:>11} {:>9} {}",
"Lv", "Compressed", "Expected", "Actual", "Delta", "Status"
);
for (level, (result, expected)) in results.iter().zip(i.overhead.iter()).enumerate() {
let expected_str = match expected {
Ok(v) => format!("{}", v),
Err(e) => format!("{:?}", e),
};
let actual_str = match &result.overhead {
Ok(v) => format!("{}", v),
Err(e) => format!("{:?}", e),
};
let (delta_str, status) = match (expected, &result.overhead) {
(Ok(exp), Ok(act)) => {
let delta = *act as i64 - *exp as i64;
let status = if delta < 0 {
"BETTER"
} else if delta > 0 {
"WORSE"
} else {
"ok"
};
(format!("{:+}", delta), status)
}
}
(exp, act) if exp == act => ("=".to_string(), "ok"),
_ => ("?".to_string(), "CHANGED"),
};
println!(
" {:>3} {:>11} {:>11} {:>11} {:>9} {}",
level, result.compressed_size, expected_str, actual_str, delta_str, status
);
}
println!();
}

// Now assert — failures will name the compressor and show the diff above.
for (i, results) in bench.iter().zip(all_results.iter()) {
let actual: Vec<_> = results.iter().map(|r| r.overhead.clone()).collect();
assert_eq!(
result, i.overhead,
actual,
i.overhead.as_ref(),
"compression overhead mismatch for {}",
i.name
);
}

for i in bench.iter() {
print!("{}: [", i.name);

for i in i.overhead.iter() {
match i {
Ok(v) => print!("{:.2}%, ", (*v as f32) * 100.0 / (original.len() as f32)),
Err(e) => print!("{:?}, ", e),
}
}
println!("]");
}
}
2 changes: 1 addition & 1 deletion util/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Parser, command};
use clap::Parser;
use cpu_time::ProcessTime;
use env_logger::Builder;
use log::LevelFilter;
Expand Down
Loading