Lock-Free Data Structures for Rust
A high-performance collection of lock-free data structures implemented in pure Rust with zero external dependencies.
Features
- ๐ World-class performance: Stack (50M+ ops/sec), Queue (70M+ ops/sec), HashMap (250M+ ops/sec for reads)
- ๐ Lock-free: No mutexes, no blocking, suitable for real-time systems
- ๐ฆ Zero dependencies: Pure Rust implementation
- ๐งต Thread-safe: All structures are Send + Sync
- ๐พ Cache-friendly: Optimized memory layout with cache-line alignment
Data Structures
Stack
- Algorithm: Treiber's algorithm
- Performance: 50-60M ops/sec
- Use cases: LIFO workloads, undo operations
- โ ๏ธ Warning: Avoid high-concurrency mixed push/pop patterns. Use producer/consumer patterns for safety.
Queue
- Algorithm: Bounded array with sequence numbers
- Performance: 70-80M ops/sec
- Use cases: FIFO workloads, task queues, producer/consumer
HashMap
- Algorithm: Robin Hood hashing with open addressing
- Performance: 15-25M ops/sec (mixed), 250M+ ops/sec (parallel reads)
- Use cases: Concurrent caching, shared state
List (Skip List)
- Algorithm: Lock-free skip list
- Performance: 5-10M ops/sec
- Use cases: Ordered collections, range queries
Quick Start
use ;
// Stack example
let stack = new;
stack.push;
assert_eq!;
// Queue example
let queue = new; // Capacity must be power of 2
queue.enqueue;
assert_eq!;
// HashMap example
let map = new;
map.insert;
assert_eq!;
Performance
All benchmarks on 8-core CPU:
| Structure | Single Thread | Multi Thread (8) | Notes |
|---|---|---|---|
| Stack | 50M ops/sec | 20M ops/sec | Use producer/consumer pattern |
| Queue | 70M ops/sec | 25M ops/sec | World-class performance |
| HashMap | 15M ops/sec | 250M ops/sec (reads) | Excellent read scaling |
| SkipList | 5-10M ops/sec | Good scaling | O(log n) operations |
Safety Notes
This library uses unsafe Rust for performance. While thoroughly tested, please note:
- Stack: Has known issues with high-concurrency mixed push/pop operations. Use producer/consumer patterns.
- Memory ordering: Uses relaxed ordering where safe for maximum performance.
- No memory reclamation: Some structures may not immediately free memory (trade-off for performance).
Building
Examples
See the examples/ directory for more usage patterns:
bench_all.rs- Comprehensive benchmarkstest_simple.rs- Basic functionality testsbench_safe.rs- Safe usage patterns
License
MIT OR Apache-2.0 (dual licensed)
Contributing
Contributions welcome! Please ensure:
- All tests pass
- Benchmarks show no regression
- Document any unsafe code
- Add tests for new features
Acknowledgments
Inspired by:
- Treiber's Stack algorithm
- Michael & Scott Queue
- Harris's List
- Split-ordered lists for hash tables