use std::rc::Rc; use std::sync::{Arc, Mutex, RwLock}; use std::cell::RefCell; use rcu_clean::{BoxRcu, RcRcu, ArcRcu}; use criterion::{Criterion, criterion_group, criterion_main}; type RcRefCell = Rc>; type BoxRefCell = Box>; type BoxMutex = Box>; type BoxRwLock = Box>; type ArcMutex = Arc>; type ArcRwLock = Arc>; macro_rules! benchme { ($name:expr, $t:ident, $new:expr, $deref:expr) => { criterion::Fun::new($name, |b,&n_to_copy| { b.iter_with_setup(|| { let mut data: Vec<$t> = Vec::new(); for i in 0..n_to_copy { data.push($new(i)); } let mut total: usize = 0; for x in data.iter() { total += $deref(x); } data[0] = $new(total); (data, total) }, |(data, truetot)| { let mut total: usize = 0; for x in data.iter() { total += $deref(x); } assert_eq!(total, truetot*2); }); }) } } fn criterion_benchmark(c: &mut Criterion) { let mut funs: Vec> = Vec::new(); funs.push(benchme!("BoxRefCell", BoxRefCell, |a| Box::new(RefCell::new(a)), |x: &BoxRefCell| -> usize { *x.borrow() })); funs.push(benchme!("RcRefCell", RcRefCell, |a| Rc::new(RefCell::new(a)), |x: &RcRefCell| -> usize { *x.borrow() })); funs.push(benchme!("BoxMutex", BoxMutex, |a| Box::new(Mutex::new(a)), |x: &BoxMutex| -> usize { *x.lock().unwrap() })); funs.push(benchme!("ArcMutex", ArcMutex, |a| Arc::new(Mutex::new(a)), |x: &ArcMutex| -> usize { *x.lock().unwrap() })); funs.push(benchme!("BoxRwLock", BoxRwLock, |a| Box::new(RwLock::new(a)), |x: &BoxRwLock| -> usize { *x.read().unwrap() })); funs.push(benchme!("ArcRwLock", ArcRwLock, |a| Arc::new(RwLock::new(a)), |x: &ArcRwLock| -> usize { *x.read().unwrap() })); funs.push(benchme!("Box", Box, Box::new, |x: &Box| **x)); funs.push(benchme!("Rc", Rc, Rc::new, |x: &Rc| **x)); funs.push(benchme!("Arc", Arc, Arc::new, |x: &Arc| **x)); funs.push(benchme!("RcRcuStale", RcRcu, |i| { let x = RcRcu::new(0); *x.update() = i; x }, |x: &RcRcu| **x as usize)); funs.push(benchme!("ArcRcuStale", ArcRcu, |i| { let x = ArcRcu::new(0); *x.update() = i; x }, |x: &ArcRcu| **x as usize)); funs.push(benchme!("BoxRcuStale", BoxRcu, |i| { let x = BoxRcu::new(0); *x.update() = i; x }, |x: &BoxRcu| **x as usize)); funs.push(benchme!("RcRcu", RcRcu, RcRcu::new, |x: &RcRcu| **x as usize)); funs.push(benchme!("ArcRcu", ArcRcu, ArcRcu::new, |x: &ArcRcu| **x as usize)); funs.push(benchme!("BoxRcu", BoxRcu, BoxRcu::new, |x: &BoxRcu| **x as usize)); funs.reverse(); c.bench_functions("sum", funs, 1000); } criterion_group!(benches, criterion_benchmark); criterion_main!(benches);