use kovan_map::HopscotchMap;
use std::sync::Arc;
use std::thread;
use std::time::Instant;
fn main() {
println!("=== Lock-Free Concurrent HashMap Demo ===\n");
let map = Arc::new(HopscotchMap::with_capacity(256));
println!("Benchmarking concurrent inserts...");
let start = Instant::now();
let mut handles = Vec::new();
for thread_id in 0..8 {
let map_clone = Arc::clone(&map);
let handle = thread::spawn(move || {
for i in 0..10_000 {
let key = thread_id * 10_000 + i;
map_clone.insert(key, key * 2);
}
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
let duration = start.elapsed();
println!(
"Inserted 80,000 entries from 8 threads in {:?} ({:.2} ops/sec)",
duration,
80_000.0 / duration.as_secs_f64()
);
println!("Map contains {} entries\n", map.len());
println!("Benchmarking concurrent reads...");
let start = Instant::now();
let mut handles = Vec::new();
for _ in 0..8 {
let map_clone = Arc::clone(&map);
let handle = thread::spawn(move || {
let mut found = 0;
for key in 0..10_000 {
if map_clone.get(&key).is_some() {
found += 1;
}
}
found
});
handles.push(handle);
}
let mut total_found = 0;
for handle in handles {
total_found += handle.join().unwrap();
}
let duration = start.elapsed();
println!(
"Performed 80,000 reads from 8 threads in {:?} ({:.2} ops/sec)",
duration,
80_000.0 / duration.as_secs_f64()
);
println!("Found {} entries during reads\n", total_found);
println!("Benchmarking mixed concurrent operations...");
let start = Instant::now();
let mut handles = Vec::new();
for _ in 0..4 {
let map_clone = Arc::clone(&map);
let handle = thread::spawn(move || {
for key in 0..5_000 {
let _ = map_clone.get(&key);
}
});
handles.push(handle);
}
for thread_id in 0..2 {
let map_clone = Arc::clone(&map);
let handle = thread::spawn(move || {
for i in 0..5_000 {
let key = 80_000 + thread_id * 5_000 + i;
map_clone.insert(key, key);
}
});
handles.push(handle);
}
for thread_id in 0..2 {
let map_clone = Arc::clone(&map);
let handle = thread::spawn(move || {
for i in 0..2_500 {
let key = thread_id * 2_500 + i;
map_clone.remove(&key);
}
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
let duration = start.elapsed();
println!("Mixed operations completed in {:?}", duration);
println!("Final map size: {} entries\n", map.len());
println!("Verifying data integrity...");
let mut verified = 0;
for key in 5_000..10_000 {
if map.get(&key) == Some(key * 2) {
verified += 1;
}
}
println!("Verified {} entries have correct values", verified);
println!("\n=== Demo Complete ===");
println!("All operations completed without any locks or blocking!");
}