Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up hash_calc selection logic #140

Merged
merged 1 commit into from
Aug 12, 2024
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
30 changes: 12 additions & 18 deletions zlib-rs/src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,17 +786,7 @@ fn lm_set_level(state: &mut State, level: i8) {
state.nice_match = CONFIGURATION_TABLE[level as usize].nice_length as usize;
state.max_chain_length = CONFIGURATION_TABLE[level as usize].max_chain as usize;

// Use rolling hash for deflate_slow algorithm with level 9. It allows us to
// properly lookup different hash chains to speed up longest_match search. Since hashing
// method changes depending on the level we cannot put this into functable. */
state.hash_calc_variant = if state.max_chain_length > 1024 {
HashCalcVariant::Roll
} else if Crc32HashCalc::is_supported() {
HashCalcVariant::Crc32
} else {
HashCalcVariant::Standard
};

state.hash_calc_variant = HashCalcVariant::for_max_chain_length(state.max_chain_length);
state.level = level;
}

Expand Down Expand Up @@ -4026,7 +4016,7 @@ mod test {
}

#[test]
fn small_aarch64_difference() {
fn hash_calc_difference() {
let input = [
0, 0, 0, 0, 0, 43, 0, 0, 0, 0, 0, 0, 43, 0, 0, 0, 0, 0, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand All @@ -4052,14 +4042,14 @@ mod test {
];

let config = DeflateConfig {
level: -1,
level: 6,
method: Method::Deflated,
window_bits: 9,
mem_level: 8,
strategy: Strategy::Default,
};

let x86 = [
let crc32 = [
24, 149, 99, 96, 96, 96, 96, 208, 6, 17, 112, 138, 129, 193, 128, 1, 29, 24, 50, 208,
1, 200, 146, 169, 79, 24, 74, 59, 96, 147, 52, 71, 22, 70, 246, 88, 26, 94, 80, 128,
83, 6, 162, 219, 144, 76, 183, 210, 5, 8, 67, 105, 7, 108, 146, 230, 216, 133, 145,
Expand All @@ -4073,10 +4063,14 @@ mod test {
160, 197, 192, 192, 96, 196, 0, 0, 3, 228, 25, 128,
];

if cfg!(any(target_arch = "x86_64", target_arch = "x86")) {
fuzz_based_test(&input, config, &x86);
} else {
fuzz_based_test(&input, config, &other);
// the output is slightly different based on what hashing algorithm is used
match HashCalcVariant::for_compression_level(config.level as usize) {
HashCalcVariant::Crc32 => {
fuzz_based_test(&input, config, &crc32);
}
HashCalcVariant::Standard | HashCalcVariant::Roll => {
fuzz_based_test(&input, config, &other);
}
}
}
}
22 changes: 21 additions & 1 deletion zlib-rs/src/deflate/hash_calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ pub enum HashCalcVariant {
Roll,
}

impl HashCalcVariant {
#[cfg(test)]
pub const fn for_compression_level(level: usize) -> Self {
let max_chain_length = crate::deflate::algorithm::CONFIGURATION_TABLE[level].max_chain;
Self::for_max_chain_length(max_chain_length as usize)
}

/// Use rolling hash for deflate_slow algorithm with level 9. It allows us to
/// properly lookup different hash chains to speed up longest_match search.
pub const fn for_max_chain_length(max_chain_length: usize) -> Self {
if max_chain_length > 1024 {
HashCalcVariant::Roll
} else if Crc32HashCalc::is_supported() {
HashCalcVariant::Crc32
} else {
HashCalcVariant::Standard
}
}
}

pub trait HashCalc {
const HASH_CALC_OFFSET: usize;
const HASH_CALC_MASK: u32;
Expand Down Expand Up @@ -116,7 +136,7 @@ impl HashCalc for RollHashCalc {
pub struct Crc32HashCalc;

impl Crc32HashCalc {
pub fn is_supported() -> bool {
pub const fn is_supported() -> bool {
if cfg!(target_arch = "x86") || cfg!(target_arch = "x86_64") {
return true;
}
Expand Down
Loading