pub struct RingBuffer<T> { /* private fields */ }Expand description
Thread-safe ring buffer for streaming data
This implementation uses VecDeque wrapped in Arc<RwLock> to provide
efficient ring buffer operations with proper indexed access for windowing
in multi-threaded scenarios.
§Features
- Thread-safe concurrent access via RwLock
- Bounded capacity with automatic overwrite of oldest elements
- Efficient O(k) sliding window operations where k is window size
- Zero unsafe code
- Direct indexed access for efficient windowing
- Shareable across threads with Arc
§Performance Characteristics
- Operations: O(1) push/pop (with locking overhead ~10-50ns per operation)
- Sliding window: O(k) where k is window size (efficient vectorized copy)
- Concurrent reads: Multiple threads can read simultaneously via RwLock
- Write contention: Only one writer at a time, may cause blocking under high contention
- Memory overhead: ~2-3x higher than
LocalRingBufferdue to Arc/RwLock wrapper
§Performance Under Contention
Low contention (few threads, infrequent writes):
- Performs well with minimal overhead
- RwLock allows efficient concurrent reads
High contention (many threads, frequent writes):
- Write operations may block waiting for lock acquisition
- Performance degrades with increasing writer contention
- Consider using separate buffers per thread if possible
§When to Use Each Implementation
Use RingBuffer when:
- Multiple threads need to access the same buffer
- Producer-consumer patterns across threads
- Shared data collection and analysis
- Thread safety is required
Use LocalRingBuffer when:
- Single-threaded processing (~2x faster)
- Performance-critical inner loops
- Local buffering in compute-intensive functions
- Thread safety is not needed
Benchmark results (1M operations):
LocalRingBuffer: ~3ms (no synchronization)RingBuffer: ~5-8ms (with RwLock overhead)- High contention: ~15-30ms (multiple writers)
Implementations§
Source§impl<T: Copy + Default> RingBuffer<T>
impl<T: Copy + Default> RingBuffer<T>
Sourcepub fn sliding_window(&self, size: usize) -> Vec<T>
pub fn sliding_window(&self, size: usize) -> Vec<T>
Get sliding window of last N elements
§Performance
This method efficiently accesses the last N elements using VecDeque’s indexed access, avoiding the need to pop and push all elements. Time complexity: O(n) where n is the window size.
Sourcepub fn get(&self, index: usize) -> Option<T>
pub fn get(&self, index: usize) -> Option<T>
Get a single element by index (0 = oldest, len-1 = newest)
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for RingBuffer<T>
impl<T> RefUnwindSafe for RingBuffer<T>
impl<T> Send for RingBuffer<T>
impl<T> Sync for RingBuffer<T>
impl<T> Unpin for RingBuffer<T>
impl<T> UnwindSafe for RingBuffer<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more