Direct Ring Buffer
This crate provides a high-performance, lock-free ring buffer for single-producer, single-consumer scenarios, where efficient writing and reading of elements is crucial.
Overview
A ring buffer is a fixed-size buffer that operates as a circular queue. This implementation uses a lock-free approach, making it suitable for real-time applications where minimal latency is important. The buffer supports efficient bulk operations through block-based reads and writes, making it ideal for scenarios with one producer and one consumer.
Features
- Single-Producer, Single-Consumer: Designed for scenarios with a single writer and a single reader.
- Lock-Free: Utilizes atomic operations for synchronization, avoiding the need for mutexes or other locking mechanisms.
- Slice-Based I/O: Supports reading and writing multiple elements at a time using slices, enhancing performance for bulk operations.
- Closure-Based Access: Provides direct access to the buffer through closures, allowing for flexible and efficient element processing.
- Single Element Read/Write: Supports not only slice-based operations but also single element read and write operations.
Type Constraints
The buffer requires the type T to implement the Copy trait because it operates on uninitialized memory. The usage of Copy depends on the operation:
- Slice-based operations like
read_slicesandwrite_slicesdo not requireCopy, allowing direct memory access without copying elements. - Single-element operations like
read_elementandwrite_elementrequireCopyto safely handle the copying of individual elements.
Example
use ;
let = ;
// Write data to the buffer in slices
producer.write_slices;
producer.write_slices;
// Read the data
consumer.read_slices;
// Test wrap-around by writing more data
producer.write_slices;
// Verify the wrap-around data
consumer.read_slices;
// Write 5 more values to test wrap-around in one go
let test_data = ;
producer.write_slices;
// Read the newly written values to verify wrap-around
consumer.read_slices;
Note: For single element operations using read_element or write_element, see the documentation in lib.rs.
Safety and Performance
This implementation uses unsafe blocks with proper checks to ensure safe access to uninitialized memory. Atomic operations are utilized for synchronization, minimizing overhead and allowing for high-performance scenarios where low-latency and real-time constraints are critical.
Optimized for Bulk Operations
This ring buffer is optimized for reading and writing multiple elements at once, reducing overhead for batch processing. While it supports single-element operations, bulk operations maximize performance and minimize overhead.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.