Expand description
A generation-tracked wrapper around ArcSwap that enables consumers to cache
Arc references locally and only reload when data has changed.
§Overview
This library optimizes read-heavy workloads where shared data is updated
infrequently. Instead of calling ArcSwap::load_full() on every access
(which involves atomic refcount operations), consumers check a cheap atomic
generation counter first and only reload when necessary.
§Example
use genswap::GenSwap;
use std::sync::Arc;
use std::thread;
// Shared config that gets updated periodically
let config = Arc::new(GenSwap::new(42u64));
// Spawn consumer threads
let handles: Vec<_> = (0..4).map(|_| {
let config = Arc::clone(&config);
thread::spawn(move || {
let mut reader = config.reader();
for _ in 0..1000 {
let value = reader.get();
// Use value - hot path: just an atomic load + comparison
assert!(**value >= 42);
}
})
}).collect();
for h in handles {
h.join().unwrap();
}Structs§
- Cached
Reader - The consumer-side handle that caches an
Arc<T>locally. - GenSwap
- The producer-side type that holds the data and generation counter.