lock-free

Crates.iolock-free
lib.rslock-free
version0.1.2
created_at2025-06-20 09:39:37.5901+00
updated_at2025-06-20 09:44:21.264562+00
descriptionHigh-performance lock-free data structures for Rust with zero dependencies
homepage
repository
max_upload_size
id1719371
size130,464
Vladyslav Chereshnovskyy (andomini)

documentation

https://docs.rs/lock-free

README

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 lock_free::{Stack, Queue, HashMap};

// Stack example
let stack = Stack::new();
stack.push(42);
assert_eq!(stack.pop(), Some(42));

// Queue example  
let queue = Queue::new(1024); // Capacity must be power of 2
queue.enqueue("hello");
assert_eq!(queue.dequeue(), Some("hello"));

// HashMap example
let map = HashMap::new();
map.insert("key", "value");
assert_eq!(map.get(&"key"), Some("value"));

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:

  1. Stack: Has known issues with high-concurrency mixed push/pop operations. Use producer/consumer patterns.
  2. Memory ordering: Uses relaxed ordering where safe for maximum performance.
  3. No memory reclamation: Some structures may not immediately free memory (trade-off for performance).

Building

cargo build --release
cargo test
cargo bench

Examples

See the examples/ directory for more usage patterns:

  • bench_all.rs - Comprehensive benchmarks
  • test_simple.rs - Basic functionality tests
  • bench_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
Commit count: 0

cargo fmt