jacques 0.1.3

High-performance lock-free MPMC queues with horizontal scaling and zero-allocation operation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! # Jacques: High-Performance Lock-Free MPMC Queues
//!
//! Jacques is a high-performance, lock-free Multi-Producer Multi-Consumer
//! (MPMC) queue library designed for concurrent applications requiring maximum
//! throughput and minimal latency.
//!
//! ## Features
//!
//! - **Lock-free algorithms**: Zero mutex contention with atomic operations
//! - **MPMC support**: Multiple producers and consumers can operate
//!   concurrently
//! - **Zero-allocation operation**: No dynamic allocation during push/pop
//!   operations
//! - **Horizontal scaling**: Pack-based load distribution across multiple
//!   queues
//! - **Type safety**: Comprehensive compile-time guarantees with generic design
//! - **Memory efficient**: Packed 128-bit atomic operations with sequence
//!   numbers
//! - **Rich API**: Blocking, non-blocking, conditional, and bulk operations
//!
//! ## Queue Types
//!
//! Jacques provides three main queue implementations:
//!
//! ### 1. Owned Queue (`MpmcQueue`)
//! The foundational lock-free queue for `Copy` types:
//!
//! ```rust
//! use jacques::{
//!     owned::queue,
//!     traits::{QueueConsumer, QueueProducer},
//! };
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), jacques::QueueError> {
//! let (producer, consumer) = queue::<u64>().capacity(1024).channels()?;
//!
//! producer.push(42)?;
//! assert_eq!(consumer.pop()?, 42);
//! # Ok(())
//! # }
//! ```
//!
//! ### 2. Pointer Queue (`PointerQueue`)
//! Store non-Copy types by wrapping them in `Arc<T>`:
//!
//! ```rust
//! use jacques::pointer::pointer_queue;
//! use std::sync::Arc;
//!
//! #[derive(Debug, Clone, PartialEq)]
//! struct Message {
//!     id: u64,
//!     data: Vec<u8>,
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), jacques::QueueError> {
//! use jacques::traits::{QueueConsumer, QueueProducer};
//! let (producer, consumer) = pointer_queue::<Message>().capacity(512).channels()?;
//!
//! let msg = Arc::new(Message {
//!     id: 1,
//!     data: vec![1, 2, 3],
//! });
//! producer.push(msg.clone())?;
//! assert_eq!(consumer.pop()?, msg);
//! # Ok(())
//! # }
//! ```
//!
//! ### 3. Queue Pack (`QueuePack`)
//! Horizontal scaling with multiple independent queues:
//!
//! ```rust
//! use jacques::pack::queue_pack;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), jacques::QueueError> {
//! // 4 queues, scan every 16 operations
//! use jacques::traits::{QueueConsumer, QueueProducer};
//! let (producer, consumer) = queue_pack::<u64, 4, 16>().queue_capacity(256).channels()?;
//!
//! producer.push(100)?;
//! assert_eq!(consumer.pop()?, 100);
//! # Ok(())
//! # }
//! ```
//!
//! ## Performance Characteristics
//!
//! - **Throughput**: >100M operations/second on modern hardware
//! - **Latency**: Sub-microsecond operation latency
//! - **Scalability**: Linear scaling with core count using queue packs
//! - **Memory**: Constant memory usage, no dynamic allocation
//!
//! ## Advanced Features
//!
//! ### Sequence Numbers
//! Track operation ordering across concurrent access:
//!
//! ```rust
//! use jacques::owned::queue;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), jacques::QueueError> {
//! use jacques::traits::{QueueConsumer, QueueProducer};
//! let (producer, consumer) = queue::<u8>().capacity(64).channels()?;
//!
//! let seq = producer.push_with_seq(12)?;
//! let (value, pop_seq) = consumer.pop_with_seq()?;
//! assert_eq!(value, 12);
//! # Ok(())
//! # }
//! ```
//!
//! ### Conditional Operations
//! Process elements based on predicates:
//!
//! ```rust
//! use jacques::owned::queue;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), jacques::QueueError> {
//! use jacques::traits::{QueueConsumer, QueueProducer};
//! let (producer, consumer) = queue::<i32>().capacity(32).channels()?;
//!
//! producer.push(2)?;
//! producer.push(1)?;
//! producer.push(3)?;
//!
//! // Pop the head if it's even
//! let even = consumer.pop_if(|&value, _seq| value % 2 == 0)?;
//! assert_eq!(even, 2);
//! # Ok(())
//! # }
//! ```
//!
//! ### Bulk Processing
//! Consume multiple elements efficiently:
//!
//! ```rust
//! use jacques::owned::queue;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), jacques::QueueError> {
//! use jacques::traits::{QueueConsumer, QueueProducer};
//! let (producer, consumer) = queue::<u32>().capacity(16).channels()?;
//!
//! for i in 0..5 {
//!     producer.push(i)?;
//! }
//!
//! let mut sum = 0;
//! let count = consumer.consume(|value, _seq| {
//!     sum += value;
//!     value >= 3 // Stop after processing value 3
//! });
//!
//! println!("Processed {} items, sum: {}", count, sum);
//! # Ok(())
//! # }
//! ```
//!
//! ## Thread Safety
//!
//! All queue types are `Send + Sync` and designed for concurrent access:
//!
//! ```rust
//! use jacques::owned::queue;
//! use std::thread;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), jacques::QueueError> {
//! use jacques::traits::{QueueConsumer, QueueProducer};
//! let (producer, consumer) = queue::<usize>().capacity(1024).channels()?;
//!
//! // Spawn producer thread
//! let producer_handle = {
//!     let producer = producer.clone();
//!     thread::spawn(move || {
//!         for i in 0..100 {
//!             producer.push(i).unwrap();
//!         }
//!     })
//! };
//!
//! // Spawn consumer thread
//! let consumer_handle = {
//!     let consumer = consumer.clone();
//!     thread::spawn(move || {
//!         let mut sum = 0;
//!         for _ in 0..100 {
//!             sum += consumer.pop().unwrap();
//!         }
//!         sum
//!     })
//! };
//!
//! producer_handle.join().unwrap();
//! let sum = consumer_handle.join().unwrap();
//! println!("Sum: {}", sum);
//! # Ok(())
//! # }
//! ```
//!
//! ## Memory Layout
//!
//! Jacques uses a carefully designed memory layout for optimal performance:
//! - 128-bit atomic operations containing both data and sequence numbers
//! - Cache-padded storage to prevent false sharing
//! - Power-of-two capacities for efficient modulo operations
//!
//! ## Error Handling
//!
//! All operations return `Result` types with descriptive errors:
//! - `QueueError::Full` - Queue capacity exceeded
//! - `QueueError::Empty` - No elements available
//! - `QueueError::InvalidCapacity` - Invalid configuration
//! - `QueueError::TypeSizeExceeded` - Type too large for atomic storage
//!
//! ## Minimum Supported Rust Version (MSRV)
//!
//! Jacques requires Rust 1.88 or later.
#![deny(
    missing_docs,
    unused_imports,
    unused_variables,
    dead_code,
    unreachable_code,
    unused_must_use
)]
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(
    clippy::module_name_repetitions,
    clippy::similar_names,
    clippy::too_many_arguments,
    clippy::must_use_candidate,
    clippy::missing_errors_doc,
    clippy::cast_precision_loss,
    clippy::type_complexity,
    clippy::similar_names
)]
#![cfg_attr(docsrs, feature(doc_cfg))]

/// Core lock-free MPMC queue implementation for `Copy` types.
///
/// This module provides the foundational [`MpmcQueue`] implementation and
/// associated builder patterns, producer/consumer handles, and convenience
/// functions.
///
/// [`MpmcQueue`]: owned::MpmcQueue
pub mod owned;

/// Horizontal scaling with multiple independent queues.
///
/// This module provides [`QueuePack`] which distributes load across multiple
/// independent queues for better performance and reduced contention in
/// high-throughput scenarios.
///
/// [`QueuePack`]: pack::QueuePack
pub mod pack;

/// Lock-free MPMC queue for non-`Copy` types using `Arc<T>` storage.
///
/// This module provides [`PointerQueue`] which enables storing arbitrary types
/// by converting `Arc<T>` values to raw pointers internally, maintaining the
/// performance characteristics of the underlying copy-based queue.
///
/// [`PointerQueue`]: pointer::PointerQueue
pub mod pointer;

/// Common traits for queue producers, consumers, and factories.
///
/// This module defines the core abstractions that enable consistent APIs across
/// all queue implementations: [`QueueProducer`], [`QueueConsumer`], and
/// [`QueueFactory`].
///
/// [`QueueProducer`]: traits::QueueProducer
/// [`QueueConsumer`]: traits::QueueConsumer
/// [`QueueFactory`]: traits::QueueFactory
pub mod traits;

use std::{mem, mem::MaybeUninit, ptr};
use thiserror::Error;

/// Errors that can occur during queue operations.
///
/// This enum provides comprehensive error reporting for all queue operations,
/// enabling robust error handling in concurrent applications.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum QueueError {
    /// The queue has reached its maximum capacity and cannot accept more
    /// elements.
    ///
    /// This error occurs when attempting to push to a full queue using
    /// non-blocking operations like `try_push`. Blocking operations will
    /// spin until space becomes available.
    #[error("queue is full")]
    Full,

    /// The queue contains no elements to consume.
    ///
    /// This error occurs when attempting to pop from an empty queue using
    /// non-blocking operations like `try_pop`. Blocking operations will
    /// spin until elements become available.
    #[error("queue is empty")]
    Empty,

    /// The specified capacity is invalid.
    ///
    /// Queue capacities must be powers of two and at least 2. This constraint
    /// enables efficient bit-masking operations for index calculations.
    #[error("invalid capacity: must be a power of two and >= 2")]
    InvalidCapacity,

    /// The combined size of data and index types exceeds the 16-byte atomic
    /// storage limit.
    ///
    /// Jacques packs data and sequence numbers into 128-bit atomic operations.
    /// The total size of your data type plus the index type must not exceed
    /// 16 bytes.
    #[error("type size constraints violated (data + index must fit in 16 bytes)")]
    TypeSizeExceeded {
        /// The size of the data type that caused the constraint violation.
        size: usize,
    },

    /// The runtime capacity does not match the compile-time capacity for static
    /// queues.
    ///
    /// When using const generic capacity parameters, the runtime capacity
    /// parameter must match the compile-time parameter exactly.
    #[error("capacity mismatch for compile-time queue")]
    CapacityMismatch,
}

/// Packs data and sequence number into a single 128-bit atomic value.
///
/// This function implements the core memory layout for Jacques queues by
/// combining both data and sequence information into a single atomic operation.
/// The layout uses the upper bits for data and lower bits for the sequence
/// number.
///
/// # Memory Layout
///
/// ```text
/// |--- Data (128-seq_shift bits) ---|--- Sequence (seq_shift bits) ---|
/// |                                  |                                  |
/// u128 = (data_u128 << seq_shift) | (seq & seq_mask)
/// ```
///
/// # Safety
///
/// This function uses `unsafe` code for byte-level copying but maintains safety
/// by:
/// - Only copying `size_of::<T>()` bytes
/// - Using a properly sized 16-byte buffer
/// - Ensuring `T: Copy` for bitwise copying safety
///
/// # Parameters
///
/// - `seq`: The sequence number to pack (only lower `seq_shift` bits used)
/// - `data`: The data value to pack
/// - `seq_shift`: Number of bits allocated for sequence numbers
///
/// # Returns
///
/// A packed 128-bit value suitable for atomic storage
#[allow(clippy::extra_unused_type_parameters)]
const fn pack_entry<T, I>(seq: u128, data: T, seq_shift: u32) -> u128
where
    T: Copy,
    I: Copy,
{
    let data_size = mem::size_of::<T>();
    let mut buf = [0u8; 16];

    // SAFETY: We copy exactly `data_size` bytes from a valid `T` reference
    // into a 16-byte buffer, which is always sufficient since we validate
    // that `size_of::<T>() + size_of::<I>() <= 16` during queue creation.
    unsafe {
        ptr::copy_nonoverlapping((&raw const data).cast::<u8>(), buf.as_mut_ptr(), data_size);
    }

    let data_u128 = u128::from_le_bytes(buf);
    let seq_mask = (1u128 << seq_shift) - 1u128;
    (data_u128 << seq_shift) | (seq & seq_mask)
}

/// Unpacks data and sequence number from a 128-bit atomic value.
///
/// This function reverses the packing operation, extracting both the sequence
/// number and data from a single atomic value while maintaining memory safety.
///
/// # Safety
///
/// This function uses `unsafe` code for byte-level copying but maintains safety
/// by:
/// - Only copying the exact size of `T` as specified by `data_size`
/// - Using `MaybeUninit` to handle uninitialized memory properly
/// - Ensuring `T: Copy` for safe bitwise reconstruction
///
/// # Parameters
///
/// - `val`: The packed 128-bit value from atomic storage
/// - `seq_shift`: Number of bits allocated for sequence numbers
/// - `data_size`: Size in bytes of the data type `T`
///
/// # Returns
///
/// A tuple containing `(sequence_number, unpacked_data)`
#[allow(clippy::extra_unused_type_parameters)]
const fn unpack_entry<T, I>(val: u128, seq_shift: u32, data_size: usize) -> (u128, T)
where
    T: Copy,
    I: Copy,
{
    let seq_mask = (1u128 << seq_shift) - 1u128;
    let seq = val & seq_mask;
    let data_u128 = val >> seq_shift;
    let bytes = data_u128.to_le_bytes();

    let mut t_uninit = MaybeUninit::<T>::uninit();

    // SAFETY: We copy exactly `data_size` bytes (which equals `size_of::<T>()`)
    // from the byte array into uninitialized memory, then assume initialization.
    // This is safe because:
    // 1. `T: Copy` guarantees bitwise copying is valid
    // 2. We copy the exact number of bytes that make up `T`
    // 3. The bytes came from a valid `T` during packing
    unsafe {
        ptr::copy_nonoverlapping(
            bytes.as_ptr(),
            t_uninit.as_mut_ptr().cast::<u8>(),
            data_size,
        );
        (seq, t_uninit.assume_init())
    }
}