Expand description
§HashMemo - Hash Value Memoization
A library for memoizing hash values of complex data structures to improve performance when the same data is hashed multiple times.
This library is particularly beneficial for large data structures where computing hash values is expensive (e.g., large strings, vectors, complex nested structures). By caching the hash value after first computation, subsequent hash operations become O(1) instead of O(n) where n is the size of the data.
§Features
- Lazy hash computation - only calculates when needed
- Thread-safe caching with atomic operations
- Minimal memory overhead with zero-sized hashers
- Works with any
BuildHasher
implementation
§Examples
use hashmemo::HashMemo;
use std::collections::HashMap;
// Wrap a large string that will be hashed multiple times
let large_data = "a".repeat(10000); // 10KB string
let memo = HashMemo::new(large_data);
// Use in HashMap - hash is computed once and cached
let mut map = HashMap::new();
map.insert(memo, "value");
// Subsequent operations using the same data are much faster
// because the hash is already cached
§Performance Benefits
The performance improvement is most significant for:
- Large strings or byte arrays
- Complex nested data structures (Vec, HashMap, etc.)
- Data that will be used as hash keys multiple times
- Concurrent scenarios where the same data is hashed by multiple threads
Structs§
- Hash
Memo - A wrapper that memoizes the hash value of its contained data.