Skip to content

Commit bf83741

Browse files
committed
Improve cache key
Use all features of the CRC definition.
1 parent 6626c6c commit bf83741

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/arch/software.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,13 @@ const RUST_CRC64_WE: crc::Crc<u64, Table<16>> = crc::Crc::<u64, Table<16>>::new(
8080
#[allow(unused)]
8181
const RUST_CRC64_XZ: crc::Crc<u64, Table<16>> = crc::Crc::<u64, Table<16>>::new(&crc::CRC_64_XZ);
8282

83-
static CUSTOM_CRC32_CACHE: OnceLock<Mutex<HashMap<u32, &'static Algorithm<u32>>>> = OnceLock::new();
84-
static CUSTOM_CRC64_CACHE: OnceLock<Mutex<HashMap<u64, &'static Algorithm<u64>>>> = OnceLock::new();
83+
static CUSTOM_CRC32_CACHE: OnceLock<Mutex<HashMap<Crc32Key, &'static Algorithm<u32>>>> =
84+
OnceLock::new();
85+
static CUSTOM_CRC64_CACHE: OnceLock<Mutex<HashMap<Crc64Key, &'static Algorithm<u64>>>> =
86+
OnceLock::new();
87+
88+
type Crc32Key = (u32, u32, bool, bool, u32, u32);
89+
type Crc64Key = (u64, u64, bool, bool, u64, u64);
8590

8691
#[allow(unused)]
8792
// Dispatch function that handles the generic case
@@ -106,7 +111,14 @@ pub(crate) fn update(state: u64, data: &[u8], params: CrcParams) -> u64 {
106111
let mut cache = cache.lock().unwrap();
107112

108113
// Create a key from params that uniquely identifies this algorithm
109-
let key = params.poly as u32;
114+
let key: Crc32Key = (
115+
params.poly as u32,
116+
params.init as u32,
117+
params.refin,
118+
params.refout,
119+
params.xorout as u32,
120+
params.check as u32,
121+
);
110122

111123
let static_algorithm = cache.entry(key).or_insert_with(|| {
112124
let algorithm = Algorithm {
@@ -141,7 +153,14 @@ pub(crate) fn update(state: u64, data: &[u8], params: CrcParams) -> u64 {
141153
let cache = CUSTOM_CRC64_CACHE.get_or_init(|| Mutex::new(HashMap::new()));
142154
let mut cache = cache.lock().unwrap();
143155

144-
let key = params.poly;
156+
let key: Crc64Key = (
157+
params.poly,
158+
params.init,
159+
params.refin,
160+
params.refout,
161+
params.xorout,
162+
params.check,
163+
);
145164

146165
let static_algorithm = cache.entry(key).or_insert_with(|| {
147166
let algorithm = Algorithm {

0 commit comments

Comments
 (0)