The best Ring Buffer library?

Which library to choose?
Do you have any experience with these libraries?

https://crates.io/crates/circular-queue
https://crates.io/crates/ringbuf
https://crates.io/crates/ringbuffer
https://crates.io/crates/ring_queue

There is also std::collections::VecDeque.

EDIT: Also dasp_ring_buffer - Rust. I would use the one from the standard library until you have a reason not to. If performance is an issue, leave it until you have your application, then benchmark different possibilities.

How to set "wrapping" for VecDeque?

VecDeque is "growable" rather than fixed size. When you do the 4th push_back, the implementation will allocate a new larger buffer (with capacity > 3), copy the existing data to it, and set the length to 3, before appending the new element and incrementing the length.

I need a constant size. And the oldest value to be overwritten.

What are the exact semantics you are looking for?

  • Will the structure be shared between threads? Will the producer and consumer be on different threads?
  • Do you know the preferred length of the buffer at compile-time, or do you need the ability to choose it at runtime?
  • Should there be a concept of "half-full buffer": do you need state to track how many of the values in the buffer are "live", or is it sufficient to always include the whole buffer, e.g. initialized to 0?
  • What operations are the most important to be fast? And how important is performance?

You can do this:

2 Likes

Careful - VecDeque will always round its capacity up to the nearest 2n − 1. For example:

use std::collections::VecDeque;
let v: VecDeque<u8> = VecDeque::with_capacity(10);
assert_eq!(v.capacity(), 15);

If you want to use arbitrary capacities, you'll need to store the capacity separately.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.