Turbo MPMC ๐
A blazingly fast lock-free Multi-Producer Multi-Consumer (MPMC) queue implementation in Rust that outperforms crossbeam-channel. Built using a ticket-based Vyukov-style bounded queue design with cache-line optimization.
Features โจ
- ๐ High Performance: Outperforms crossbeam-channel in most scenarios
- ๐ Lock-Free: Uses atomic operations for thread-safe communication
- ๐ช MPMC Support: Multiple producers and consumers can work simultaneously
- ๐ฏ Cache-Optimized: Cache-line aligned slots prevent false sharing
- โก Zero-Copy: Efficient memory management with minimal overhead
- ๐ก๏ธ Type-Safe: Leverages Rust's type system for compile-time safety
- ๐งช Well-Tested: Includes integration tests and Loom-based concurrency tests
Quick Start
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Basic Usage
use Queue;
use Arc;
use thread;
API Overview
Core Methods
Blocking Operations
send(value: T)- Blocks until the value is successfully sent (spins when queue is full)recv() -> Result<T, RecvError>- Blocks until a value is received (spins when queue is empty)
Non-Blocking Operations
try_send(value: T) -> Result<(), SendError<T>>- Returns immediately with error if queue is fulltry_recv() -> Result<T, RecvError>- Returns immediately with error if queue is empty
Queue Creation
// CAP must be > 0 and a power of 2
let queue = new;
Performance ๐
Benchmarks comparing against popular Rust MPMC implementations:
1 Producer, 1 Consumer (1p_1c)
- turbo_mpmc: ~50-100% faster than crossbeam-channel
- Lower latency and higher throughput
4 Producers, 4 Consumers (4p_4c)
- turbo_mpmc: Maintains performance under high contention
- Better cache utilization through aligned slots
Run Benchmarks
Benchmark results are saved to target/criterion/report/index.html
Examples
The repository includes several examples demonstrating different use cases:
Simple Example
Work Queue Pattern
Quick Performance Test
Architecture ๐๏ธ
Design Highlights
- Vyukov-Style Queue: Based on Dmitry Vyukov's bounded MPMC queue algorithm
- Ticket-Based System: Uses
fetch_addfor lock-free ticket distribution - Cache-Line Alignment: Each slot is 64-byte aligned to prevent false sharing
- Bounded Queue: Fixed capacity set at compile time (must be power of 2)
- Spin-Waiting: Optimized spinning with yields for better CPU utilization
Memory Layout
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ CachePadded<AtomicUsize> tail โ Producer ticket counter
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ CachePadded<AtomicUsize> head โ Consumer ticket counter
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Slot<T> [0] (64-byte aligned) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Slot<T> [1] (64-byte aligned) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ... โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Slot<T> [CAP-1] (64-byte aligned) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Testing ๐งช
Run Tests
# Standard tests
# Loom-based concurrency tests (slow but thorough)
RUSTFLAGS="--cfg loom"
Integration Tests
The project includes comprehensive integration tests covering:
- Single producer, single consumer scenarios
- Multiple producers, multiple consumers
- Capacity verification
- Blocking and non-blocking operations
- Edge cases and error handling
Development
Requirements
- Rust 1.70 or later
- Cargo
Build
# Debug build
# Release build (optimized)
Verification Scripts
# Linux/macOS
# Windows
Comparison with Alternatives
| Feature | turbo-mpmc | crossbeam-channel | flume | std::mpsc |
|---|---|---|---|---|
| MPMC Support | โ | โ | โ | โ (MPSC only) |
| Lock-Free | โ | โ | โ | โ |
| Bounded | โ | โ | โ | โ |
| Unbounded | โ | โ | โ | โ |
| Performance | ๐ Fastest | Fast | Fast | Moderate |
| Select Support | โ | โ | โ | โ |
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is dual-licensed under either:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
at your option.
Acknowledgments
- Based on Dmitry Vyukov's bounded MPMC queue design
- Inspired by crossbeam-channel and other high-performance queue implementations
- Thanks to the Rust community for excellent concurrency primitives
Links
- Repository: https://github.com/Sumit99589/turbo_mpmc
- Documentation: docs.rs (coming soon)
- Crate: crates.io (coming soon)
Made with โค๏ธ and Rust