kovan_map/lib.rs
1#![doc(
2 html_logo_url = "https://raw.githubusercontent.com/vertexclique/kovan/master/art/kovan-square.svg"
3)]
4//! A high-performance lock-free concurrent hash map built on top of the Kovan memory reclamation system.
5//!
6//! This crate provides two lock-free hash map implementations:
7//!
8//! - [`HashMap`]: A chaining-based hash map with fixed capacity and minimal collisions
9//! - [`HopscotchMap`]: A growing/shrinking hopscotch hash map with dynamic resizing
10//!
11//! Both implementations are thread-safe and allow concurrent reads, writes,
12//! and deletions without any locks or blocking. They achieve this through:
13//!
14//! - **Lock-free algorithms**: All operations use compare-and-swap (CAS) based techniques
15//! - **Kovan memory reclamation**: Safe memory management in concurrent environments without garbage collection
16//! - **FoldHash**: Fast, high-quality hashing for minimal collision rates
17//! - **Large bucket arrays**: Pre-allocated oversized tables (524,288 buckets) that fit in L3 cache
18//!
19//! # Features
20//!
21//! - `std` (default): Enables standard library support for additional functionality
22//! - `no_std` compatible when the `std` feature is disabled
23//!
24//! # Performance Characteristics
25//!
26//! The hash map is optimized for high-throughput concurrent workloads:
27//!
28//! - **Memory footprint**: ~4MB for the bucket array (fits in modern L3 caches)
29//! - **Load factor**: ~0.19 at 100k items with 524k buckets
30//! - **Collision rate**: Near-zero collisions in typical workloads
31//! - **Lookup performance**: Often a single pointer dereference due to minimal collisions
32//! - **Cache-optimized**: Node layout ordered for efficient CPU cache line usage
33//!
34//! # Architecture
35//!
36//! The implementation uses:
37//! - **Bucket array**: Fixed-size array of atomic pointers (null-initialized)
38//! - **Collision resolution**: Singly-linked lists for hash collisions
39//! - **Node layout**: `hash -> key -> value -> next` for optimal cache performance
40//! - **Updates**: Copy-on-write style replacements for existing keys
41//!
42//! # Example
43//!
44//! ```rust
45//! use kovan_map::HashMap;
46//! use std::sync::Arc;
47//! use std::thread;
48//!
49//! // Create a shared hash map
50//! let map = Arc::new(HashMap::new());
51//!
52//! // Spawn multiple threads for concurrent inserts
53//! let mut handles = vec![];
54//! for thread_id in 0..4 {
55//! let map = Arc::clone(&map);
56//! let handle = thread::spawn(move || {
57//! for i in 0..1000 {
58//! let key = thread_id * 1000 + i;
59//! map.insert(key, key * 2);
60//! }
61//! });
62//! handles.push(handle);
63//! }
64//!
65//! // Wait for all threads to complete
66//! for handle in handles {
67//! handle.join().unwrap();
68//! }
69//!
70//! // Read values from any thread
71//! assert_eq!(map.get(&0), Some(0));
72//! assert_eq!(map.get(&1000), Some(2000));
73//!
74//! // Remove values
75//! assert_eq!(map.remove(&0), Some(0));
76//! assert_eq!(map.remove(&0), None);
77//! ```
78//!
79//! # Limitations
80//!
81//! - **Copy values**: Currently requires `V: Copy` for simplicity and performance
82//! - **Clone keys**: Keys must implement `Clone` for update operations
83//! - **Static lifetimes**: Both `K` and `V` must be `'static` for safe memory reclamation
84//! - **Fixed capacity**: Uses a pre-allocated bucket array (cannot grow dynamically)
85//!
86//! # Use Cases
87//!
88//! This hash map is ideal for:
89//! - High-throughput concurrent caching
90//! - Lock-free data structures in systems programming
91//! - Real-time applications where blocking is unacceptable
92//! - Scenarios with many readers and occasional writers
93//!
94//! # Safety
95//!
96//! The implementation uses `unsafe` code internally for performance but provides a safe API.
97//! Memory safety is guaranteed through the Kovan memory reclamation system, which ensures
98//! nodes are only deallocated when no threads are accessing them.
99
100#![warn(missing_docs)]
101#![no_std]
102
103mod hashmap;
104mod hopscotch;
105
106pub use hashmap::*;
107pub use hopscotch::*;