| 1 |
# Implementation Plan |
| 2 |
|
| 3 |
- [x] 1. Create cache module with core data structures |
| 4 |
- Create `src/cache.rs` module file |
| 5 |
- Define `CrcParamsCacheKey` struct with `width`, `poly`, and `reflected` fields |
| 6 |
- Implement `Debug`, `Clone`, `PartialEq`, `Eq`, and `Hash` traits for the cache key |
| 7 |
- Add module declaration to `src/lib.rs` |
| 8 |
- _Requirements: 1.1, 3.1, 3.2_ |
| 9 |
|
| 10 |
- [x] 2. Implement thread-safe cache storage |
| 11 |
- Define global cache using `std::sync::OnceLock<RwLock<HashMap<CrcParamsCacheKey, [u64; 23]>>>` |
| 12 |
- Implement `get_cache()` function to initialize and return cache reference |
| 13 |
- Add necessary imports for `std::collections::HashMap`, `std::sync::{OnceLock, RwLock}` |
| 14 |
- _Requirements: 1.3, 2.3_ |
| 15 |
|
| 16 |
- [x] 3. Implement cache lookup and storage functions |
| 17 |
- Create `get_or_generate_keys(width: u8, poly: u64, reflected: bool) -> [u64; 23]` function |
| 18 |
- Implement cache hit path with read lock and HashMap lookup |
| 19 |
- Implement cache miss path with key generation followed by write lock and storage |
| 20 |
- Add error handling for lock poisoning with fallback to direct key generation |
| 21 |
- _Requirements: 1.1, 1.2, 4.1_ |
| 22 |
|
| 23 |
- [x] 4. Add cache management utilities |
| 24 |
- Implement `clear_cache()` function for testing and memory management |
| 25 |
- Add proper error handling for all cache operations |
| 26 |
- Ensure all cache operations are best-effort with graceful degradation |
| 27 |
- _Requirements: 4.3_ |
| 28 |
|
| 29 |
- [x] 5. Integrate cache into CrcParams::new() |
| 30 |
- Modify `CrcParams::new()` in `src/structs.rs` to use `cache::get_or_generate_keys()` |
| 31 |
- Replace direct call to `generate::keys()` with cache lookup |
| 32 |
- Ensure all existing functionality remains unchanged |
| 33 |
- Verify that the function signature and behavior are identical |
| 34 |
- _Requirements: 1.1, 1.2, 5.1, 5.2, 5.3_ |
| 35 |
|
| 36 |
- [x] 6. Create comprehensive unit tests for cache functionality |
| 37 |
- Add tests to `src/cache.rs` |
| 38 |
- Write tests for cache key creation, equality, and hashing |
| 39 |
- Test cache hit scenarios (same parameters return cached keys) |
| 40 |
- Test cache miss scenarios (new parameters generate and cache keys) |
| 41 |
- Test that cached keys are identical to directly generated keys |
| 42 |
- _Requirements: 1.1, 1.2, 3.1, 3.2_ |
| 43 |
|
| 44 |
- [x] 7. Add thread safety tests |
| 45 |
- Write concurrent access tests using `std::thread` |
| 46 |
- Test multiple threads reading from cache simultaneously |
| 47 |
- Test read-write contention scenarios |
| 48 |
- Verify cache consistency under concurrent access |
| 49 |
- Test lock poisoning recovery behavior |
| 50 |
- _Requirements: 1.3_ |
| 51 |
|
| 52 |
- [x] 8. Create integration tests for CrcParams compatibility |
| 53 |
- Add tests to verify `CrcParams::new()` behavior is unchanged |
| 54 |
- Test that all existing CRC parameter combinations work correctly |
| 55 |
- Verify that cached and uncached results are identical |
| 56 |
- Test multiple `CrcParams` instances with same parameters use cached keys |
| 57 |
- _Requirements: 5.1, 5.2, 5.3_ |
| 58 |
|
| 59 |
- [x] 9. Add comprehensive error handling tests |
| 60 |
- Test cache behavior when locks are poisoned |
| 61 |
- Test memory allocation failure scenarios |
| 62 |
- Verify fallback to direct key generation works correctly |
| 63 |
- Test cache operations under memory pressure |
| 64 |
- _Requirements: 4.1, 4.2_ |
| 65 |
|
| 66 |
- [x] 10. Update existing tests to work with caching |
| 67 |
- Run all existing tests to ensure no regressions |
| 68 |
- Update any tests that might be affected by caching behavior |
| 69 |
- Ensure test isolation by clearing cache between tests if needed |
| 70 |
- Verify all CRC algorithm tests still pass |
| 71 |
- _Requirements: 5.1, 5.2, 5.3_ |
| 72 |
|
| 73 |
- [x] 11. Add documentation and finalize implementation |
| 74 |
- Add inline documentation for all new public and internal functions |
| 75 |
- Update module-level documentation |
| 76 |
- Add usage examples in code comments |
| 77 |
- Ensure all code follows existing project style and conventions |
| 78 |
- _Requirements: 5.3_ |