Skip to content

Commit 243336e

Browse files
committed
Implement cache lookup and storage functions
- [x] 3. Implement cache lookup and storage functions - Create `get_or_generate_keys(width: u8, poly: u64, reflected: bool) -> [u64; 23]` function - Implement cache hit path with read lock and HashMap lookup - Implement cache miss path with key generation followed by write lock and storage - Add error handling for lock poisoning with fallback to direct key generation
1 parent 781d754 commit 243336e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

.kiro/specs/crc-params-caching/tasks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
- Add necessary imports for `std::collections::HashMap`, `std::sync::{OnceLock, RwLock}`
1414
- _Requirements: 1.3, 2.3_
1515

16-
- [ ] 3. Implement cache lookup and storage functions
16+
- [x] 3. Implement cache lookup and storage functions
1717
- Create `get_or_generate_keys(width: u8, poly: u64, reflected: bool) -> [u64; 23]` function
1818
- Implement cache hit path with read lock and HashMap lookup
1919
- Implement cache miss path with key generation followed by write lock and storage

src/cache.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashMap;
22
use std::sync::{OnceLock, RwLock};
3+
use crate::generate;
34

45
/// Global cache storage for CRC parameter keys
56
static CACHE: OnceLock<RwLock<HashMap<CrcParamsCacheKey, [u64; 23]>>> = OnceLock::new();
@@ -30,4 +31,33 @@ impl CrcParamsCacheKey {
3031
/// Uses OnceLock to ensure thread-safe lazy initialization
3132
fn get_cache() -> &'static RwLock<HashMap<CrcParamsCacheKey, [u64; 23]>> {
3233
CACHE.get_or_init(|| RwLock::new(HashMap::new()))
34+
}
35+
36+
/// Get cached keys or generate and cache them if not present
37+
///
38+
/// This function implements a read-then-write pattern for optimal performance:
39+
/// 1. First attempts a read lock to check for cached keys
40+
/// 2. If cache miss, generates keys outside of any lock
41+
/// 3. Then acquires write lock to store the generated keys
42+
///
43+
/// Falls back to direct key generation if lock poisoning occurs
44+
pub fn get_or_generate_keys(width: u8, poly: u64, reflected: bool) -> [u64; 23] {
45+
let cache_key = CrcParamsCacheKey::new(width, poly, reflected);
46+
47+
// Try cache read first - multiple threads can read simultaneously
48+
if let Ok(cache) = get_cache().read() {
49+
if let Some(keys) = cache.get(&cache_key) {
50+
return *keys;
51+
}
52+
}
53+
54+
// Generate keys outside of write lock to minimize lock hold time
55+
let keys = generate::keys(width, poly, reflected);
56+
57+
// Try to cache the result (best effort - if this fails, we still return valid keys)
58+
if let Ok(mut cache) = get_cache().write() {
59+
cache.insert(cache_key, keys);
60+
}
61+
62+
keys
3363
}

0 commit comments

Comments
 (0)