use std::sync::{Arc, RwLock}; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use arc_swap_simple::AtomicSwap; #[derive(Debug)] pub struct V{ pub v: u32 } fn load_atomic_swap(v: AtomicSwap) { let v = v.load(); black_box(v.v); } fn update_atomic_swap(v: AtomicSwap) { v.swap(V{v: 42}); } fn load_arc_mutex(v: Arc>>){ let guard = v.read().unwrap(); black_box(guard.v); } fn update_arc_mutex(v: Arc>>){ let mut guard = v.write().unwrap(); *guard = Arc::new(V{v: 42}); } fn criterion_benchmark(c: &mut Criterion) { let v = AtomicSwap::from_value(V{v: 42}); let v2 = Arc::new(RwLock::new(Arc::new(V{v:42}))); c.bench_function("load atomic swap", |b|b.iter(||load_atomic_swap(black_box(&v).clone()))); c.bench_function("load arc mutex", |b|b.iter(||load_arc_mutex(black_box(&v2).clone()))); c.bench_function("update atomic swap", |b|b.iter(||update_atomic_swap(black_box(&v).clone()))); c.bench_function("update arc mutex", |b|b.iter(||update_arc_mutex(black_box(&v2).clone()))); } criterion_group!(benches, criterion_benchmark); criterion_main!(benches);