Magic Buffers and io_uring Registered Buffers
13 points by mpweiher
13 points by mpweiher
I use double-mapped ring buffers a bunch. Good to know it'll work across io-uring if I get around to using it.
Does this “magic ring buffer” concept (with or without io_uring) affect performance in any way (positive or negative)?
It depends on what you're trying to do.
The key property is that you can expose a logically contiguous slice of a deque as one virtually contiguous slice as opposed to two slices. E.g. Rust's VecDeque is backed by a conventional (non-magic) ring buffer with a constant time method as_slices(&self) -> (&[T], &[T]) but if you want to expose it as a single contiguous slice you have to rotate the ring buffer first (which takes up to linear time and interferes with concurrency) via the make_contiguous(&mut self) -> &mut [T] method. Whereas with the magic ring buffer trick you can offer a constant time, concurrency friendly as_slice(&self) -> &[T] method.
But it's a pretty specialized data structure and I've mostly used it in a few cases where the single-slice property was useful for interoperability with other parts of the system. In many use cases you can achieve the same read-side result without virtual memory tricks but at greater write-side cost by mirroring writes, e.g. q.push_back(x) writes x at position i and at position i + n of a physically (not just virtually) double-sized buffer.