2026-03-02 17:48:03 +00:00
|
|
|
use crate::compat::Arc;
|
2026-02-06 11:25:37 +00:00
|
|
|
|
|
|
|
|
use crossbeam_queue::{ArrayQueue, SegQueue};
|
feat: Stress tests, benchmarking, and non-failing queues and inboxes #3
Adds some basic benchmarking, stress tests. They still need to be properly examined to ensure they are testing the correct properties, but fit for "good enough". Implements the HybridChannel type, which features a channel buffer that can withstand overflows. It does so by providing a dequeue behind a mutex. Without overflow, will push messages into the lock free ArrayQueue implemented by crossbeam_queue; when that buffer fills, will use the locking portion provided by the Mutex<VecDequeue>.
In the future we can even further optimize this, perhaps with some linked list implementations of lock-free channels, but, like the benchmarks, this fits the "good enough" bar for now.
2026-01-26 07:14:00 +00:00
|
|
|
|
|
|
|
|
pub struct HybridChannel<T> {
|
|
|
|
|
ring: ArrayQueue<T>,
|
2026-02-06 11:25:37 +00:00
|
|
|
overflow: SegQueue<T>,
|
feat: Stress tests, benchmarking, and non-failing queues and inboxes #3
Adds some basic benchmarking, stress tests. They still need to be properly examined to ensure they are testing the correct properties, but fit for "good enough". Implements the HybridChannel type, which features a channel buffer that can withstand overflows. It does so by providing a dequeue behind a mutex. Without overflow, will push messages into the lock free ArrayQueue implemented by crossbeam_queue; when that buffer fills, will use the locking portion provided by the Mutex<VecDequeue>.
In the future we can even further optimize this, perhaps with some linked list implementations of lock-free channels, but, like the benchmarks, this fits the "good enough" bar for now.
2026-01-26 07:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> HybridChannel<T> {
|
|
|
|
|
pub fn new(capacity: usize) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
ring: ArrayQueue::new(capacity),
|
2026-02-06 11:25:37 +00:00
|
|
|
overflow: SegQueue::new(),
|
feat: Stress tests, benchmarking, and non-failing queues and inboxes #3
Adds some basic benchmarking, stress tests. They still need to be properly examined to ensure they are testing the correct properties, but fit for "good enough". Implements the HybridChannel type, which features a channel buffer that can withstand overflows. It does so by providing a dequeue behind a mutex. Without overflow, will push messages into the lock free ArrayQueue implemented by crossbeam_queue; when that buffer fills, will use the locking portion provided by the Mutex<VecDequeue>.
In the future we can even further optimize this, perhaps with some linked list implementations of lock-free channels, but, like the benchmarks, this fits the "good enough" bar for now.
2026-01-26 07:14:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 07:35:58 +00:00
|
|
|
pub fn push(&self, value: T) {
|
2026-02-09 07:24:16 +00:00
|
|
|
if !self.overflow.is_empty() {
|
|
|
|
|
self.overflow.push(value);
|
2026-02-10 07:35:58 +00:00
|
|
|
return;
|
2026-02-09 07:24:16 +00:00
|
|
|
}
|
2026-02-10 07:35:58 +00:00
|
|
|
if let Err(v) = self.ring.push(value) {
|
|
|
|
|
self.overflow.push(v);
|
feat: Stress tests, benchmarking, and non-failing queues and inboxes #3
Adds some basic benchmarking, stress tests. They still need to be properly examined to ensure they are testing the correct properties, but fit for "good enough". Implements the HybridChannel type, which features a channel buffer that can withstand overflows. It does so by providing a dequeue behind a mutex. Without overflow, will push messages into the lock free ArrayQueue implemented by crossbeam_queue; when that buffer fills, will use the locking portion provided by the Mutex<VecDequeue>.
In the future we can even further optimize this, perhaps with some linked list implementations of lock-free channels, but, like the benchmarks, this fits the "good enough" bar for now.
2026-01-26 07:14:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn pop(&self) -> Option<T> {
|
2026-02-09 19:05:37 +00:00
|
|
|
self.ring.pop().or_else(|| self.overflow.pop())
|
feat: Stress tests, benchmarking, and non-failing queues and inboxes #3
Adds some basic benchmarking, stress tests. They still need to be properly examined to ensure they are testing the correct properties, but fit for "good enough". Implements the HybridChannel type, which features a channel buffer that can withstand overflows. It does so by providing a dequeue behind a mutex. Without overflow, will push messages into the lock free ArrayQueue implemented by crossbeam_queue; when that buffer fills, will use the locking portion provided by the Mutex<VecDequeue>.
In the future we can even further optimize this, perhaps with some linked list implementations of lock-free channels, but, like the benchmarks, this fits the "good enough" bar for now.
2026-01-26 07:14:00 +00:00
|
|
|
}
|
2026-02-25 11:11:03 +00:00
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
self.ring.is_empty() && self.overflow.is_empty()
|
|
|
|
|
}
|
feat: Stress tests, benchmarking, and non-failing queues and inboxes #3
Adds some basic benchmarking, stress tests. They still need to be properly examined to ensure they are testing the correct properties, but fit for "good enough". Implements the HybridChannel type, which features a channel buffer that can withstand overflows. It does so by providing a dequeue behind a mutex. Without overflow, will push messages into the lock free ArrayQueue implemented by crossbeam_queue; when that buffer fills, will use the locking portion provided by the Mutex<VecDequeue>.
In the future we can even further optimize this, perhaps with some linked list implementations of lock-free channels, but, like the benchmarks, this fits the "good enough" bar for now.
2026-01-26 07:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) struct Receiver<T> {
|
|
|
|
|
queue: Arc<HybridChannel<T>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Receiver<T> {
|
|
|
|
|
pub fn new(capacity: usize) -> Self {
|
|
|
|
|
let queue = Arc::new(HybridChannel::new(capacity));
|
|
|
|
|
|
|
|
|
|
Self { queue }
|
|
|
|
|
}
|
|
|
|
|
pub fn try_recv(&self) -> Option<T> {
|
2026-02-09 19:05:37 +00:00
|
|
|
self.queue.pop()
|
feat: Stress tests, benchmarking, and non-failing queues and inboxes #3
Adds some basic benchmarking, stress tests. They still need to be properly examined to ensure they are testing the correct properties, but fit for "good enough". Implements the HybridChannel type, which features a channel buffer that can withstand overflows. It does so by providing a dequeue behind a mutex. Without overflow, will push messages into the lock free ArrayQueue implemented by crossbeam_queue; when that buffer fills, will use the locking portion provided by the Mutex<VecDequeue>.
In the future we can even further optimize this, perhaps with some linked list implementations of lock-free channels, but, like the benchmarks, this fits the "good enough" bar for now.
2026-01-26 07:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-25 11:11:03 +00:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
self.queue.is_empty()
|
|
|
|
|
}
|
|
|
|
|
|
feat: Stress tests, benchmarking, and non-failing queues and inboxes #3
Adds some basic benchmarking, stress tests. They still need to be properly examined to ensure they are testing the correct properties, but fit for "good enough". Implements the HybridChannel type, which features a channel buffer that can withstand overflows. It does so by providing a dequeue behind a mutex. Without overflow, will push messages into the lock free ArrayQueue implemented by crossbeam_queue; when that buffer fills, will use the locking portion provided by the Mutex<VecDequeue>.
In the future we can even further optimize this, perhaps with some linked list implementations of lock-free channels, but, like the benchmarks, this fits the "good enough" bar for now.
2026-01-26 07:14:00 +00:00
|
|
|
pub fn new_sender(&self) -> Sender<T> {
|
|
|
|
|
Sender {
|
|
|
|
|
queue: self.queue.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) struct Sender<T> {
|
|
|
|
|
queue: Arc<HybridChannel<T>>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 04:44:46 +00:00
|
|
|
impl<T> Clone for Sender<T> {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
queue: self.queue.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
feat: Stress tests, benchmarking, and non-failing queues and inboxes #3
Adds some basic benchmarking, stress tests. They still need to be properly examined to ensure they are testing the correct properties, but fit for "good enough". Implements the HybridChannel type, which features a channel buffer that can withstand overflows. It does so by providing a dequeue behind a mutex. Without overflow, will push messages into the lock free ArrayQueue implemented by crossbeam_queue; when that buffer fills, will use the locking portion provided by the Mutex<VecDequeue>.
In the future we can even further optimize this, perhaps with some linked list implementations of lock-free channels, but, like the benchmarks, this fits the "good enough" bar for now.
2026-01-26 07:14:00 +00:00
|
|
|
impl<T> Sender<T> {
|
2026-02-10 07:35:58 +00:00
|
|
|
pub fn send(&self, value: T) {
|
2026-02-09 19:05:37 +00:00
|
|
|
self.queue.push(value)
|
feat: Stress tests, benchmarking, and non-failing queues and inboxes #3
Adds some basic benchmarking, stress tests. They still need to be properly examined to ensure they are testing the correct properties, but fit for "good enough". Implements the HybridChannel type, which features a channel buffer that can withstand overflows. It does so by providing a dequeue behind a mutex. Without overflow, will push messages into the lock free ArrayQueue implemented by crossbeam_queue; when that buffer fills, will use the locking portion provided by the Mutex<VecDequeue>.
In the future we can even further optimize this, perhaps with some linked list implementations of lock-free channels, but, like the benchmarks, this fits the "good enough" bar for now.
2026-01-26 07:14:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|