Implementation Plan
-
1. Create cache module with core data structures
- Create
src/cache.rsmodule file - Define
CrcParamsCacheKeystruct withwidth,poly, andreflectedfields - Implement
Debug,Clone,PartialEq,Eq, andHashtraits for the cache key - Add module declaration to
src/lib.rs - Requirements: 1.1, 3.1, 3.2
- Create
-
2. Implement thread-safe cache storage
- Define global cache using
std::sync::OnceLock<RwLock<HashMap<CrcParamsCacheKey, [u64; 23]>>> - Implement
get_cache()function to initialize and return cache reference - Add necessary imports for
std::collections::HashMap,std::sync::{OnceLock, RwLock} - Requirements: 1.3, 2.3
- Define global cache using
-
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
- Requirements: 1.1, 1.2, 4.1
- Create
-
4. Add cache management utilities
- Implement
clear_cache()function for testing and memory management - Add proper error handling for all cache operations
- Ensure all cache operations are best-effort with graceful degradation
- Requirements: 4.3
- Implement
-
5. Integrate cache into CrcParams::new()
- Modify
CrcParams::new()insrc/structs.rsto usecache::get_or_generate_keys() - Replace direct call to
generate::keys()with cache lookup - Ensure all existing functionality remains unchanged
- Verify that the function signature and behavior are identical
- Requirements: 1.1, 1.2, 5.1, 5.2, 5.3
- Modify
-
6. Create comprehensive unit tests for cache functionality
- Add tests to
src/cache.rs - Write tests for cache key creation, equality, and hashing
- Test cache hit scenarios (same parameters return cached keys)
- Test cache miss scenarios (new parameters generate and cache keys)
- Test that cached keys are identical to directly generated keys
- Requirements: 1.1, 1.2, 3.1, 3.2
- Add tests to
-
7. Add thread safety tests
- Write concurrent access tests using
std::thread - Test multiple threads reading from cache simultaneously
- Test read-write contention scenarios
- Verify cache consistency under concurrent access
- Test lock poisoning recovery behavior
- Requirements: 1.3
- Write concurrent access tests using
-
8. Create integration tests for CrcParams compatibility
- Add tests to verify
CrcParams::new()behavior is unchanged - Test that all existing CRC parameter combinations work correctly
- Verify that cached and uncached results are identical
- Test multiple
CrcParamsinstances with same parameters use cached keys - Requirements: 5.1, 5.2, 5.3
- Add tests to verify
-
9. Add comprehensive error handling tests
- Test cache behavior when locks are poisoned
- Test memory allocation failure scenarios
- Verify fallback to direct key generation works correctly
- Test cache operations under memory pressure
- Requirements: 4.1, 4.2
-
10. Update existing tests to work with caching
- Run all existing tests to ensure no regressions
- Update any tests that might be affected by caching behavior
- Ensure test isolation by clearing cache between tests if needed
- Verify all CRC algorithm tests still pass
- Requirements: 5.1, 5.2, 5.3
-
11. Add documentation and finalize implementation
- Add inline documentation for all new public and internal functions
- Update module-level documentation
- Add usage examples in code comments
- Ensure all code follows existing project style and conventions
- Requirements: 5.3