17 releases

0.2.2 Jan 27, 2026
0.2.0 Nov 22, 2025
0.1.13 Feb 10, 2025
0.1.9 Dec 30, 2022
0.1.3 Jul 27, 2022

#98 in Concurrency

Download history 26556/week @ 2025-10-25 28133/week @ 2025-11-01 29306/week @ 2025-11-08 29193/week @ 2025-11-15 28306/week @ 2025-11-22 30779/week @ 2025-11-29 31796/week @ 2025-12-06 34091/week @ 2025-12-13 15618/week @ 2025-12-20 12334/week @ 2025-12-27 27272/week @ 2026-01-03 33062/week @ 2026-01-10 31466/week @ 2026-01-17 29214/week @ 2026-01-24 28941/week @ 2026-01-31 35521/week @ 2026-02-07

131,433 downloads per month
Used in 91 crates (via zenoh-transport)

EPL-2.0 OR Apache-2.0

16KB
156 lines

ringbuffer-spsc

CI docs.rs Release License License

A fast #[no_std] single-producer single-consumer (SPSC) ring buffer designed for low-latency and high-throughput scenarios.

For performance reasons, the buffer's capacity must be a power of two. Using a power-of-two capacity lets the implementation wrap indices with a simple bitmask instead of using a slower modulo operation. This reduces computational overhead and improves throughput.

Example

A minimal example showing a simple producer–consumer (cross-thread) pattern.

use ringbuffer_spsc::ringbuffer;

fn main() {
    // Create a writer/reader pair
    let (mut writer, mut reader) = ringbuffer::<usize>(16);

    // Thread that pushes elements on the ringbuffer
    std::thread::spawn(move || for i in 0..usize::MAX {
        // Attempt to push an element
        if writer.push(i).is_some() {
            // The ringbuffer is full, yield the thread
            std::thread::yield_now();
        }
    });

    // Loop that pulls elements from the ringbuffer
    loop {
        match reader.pull() {
            // We have got an element, do something
            Some(t) => std::hint::blackbox(t),
            // The ringbuffer is empty, yield the thread
            None => std::thread::yield_now(),
        }
    }
}

Performance

The repository includes a throughput example. To run it locally:

cargo run --release --example throughput

Provides ~520M elem/s of sustained throughput when benchmarking the example on an Apple M4, 32 GB of RAM:

531933452 elem/s
531134948 elem/s
528573235 elem/s
529276820 elem/s

Dependencies

~105KB