From bde1a54fc0f88bc0adafd2ff26a18c76ed3d8244 Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Wed, 26 Feb 2020 16:44:00 -0500 Subject: [PATCH] Don't re-pin on every operation --- Cargo.toml | 2 +- benches/bustle.rs | 60 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e79dfbc8..d556e773 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ default-features = false rand = "0.7" rayon = "1.3" criterion = "0.3" -bustle = "0.1" +bustle = "0.2" [profile.bench] debug = true diff --git a/benches/bustle.rs b/benches/bustle.rs index 0fdafce2..d3e4bd2f 100644 --- a/benches/bustle.rs +++ b/benches/bustle.rs @@ -1,35 +1,75 @@ use bustle::*; use flurry::*; +use std::hash::Hash; use std::sync::Arc; -#[derive(Clone)] -struct Table(Arc>); +const REPIN_EVERY: usize = 1024; -impl BenchmarkTarget for Table +struct Table(Arc>); + +impl Collection for Table where - K: Sync + Send + From + Copy + 'static + std::hash::Hash + Eq + std::fmt::Debug, + K: Sync + Send + 'static + From + Copy + Hash + Eq, { - type Key = K; + type Handle = TableHandle; fn with_capacity(capacity: usize) -> Self { Self(Arc::new(HashMap::with_capacity(capacity))) } + fn pin(&self) -> Self::Handle { + let map = Arc::clone(&self.0); + let guard = map.guard(); + TableHandle { + map, + guard, + i: REPIN_EVERY, + } + } +} + +struct TableHandle { + map: Arc>, + guard: epoch::Guard, + i: usize, +} + +impl TableHandle { + #[inline(always)] + fn maybe_repin(&mut self) { + self.i -= 1; + if self.i == 0 { + self.i = REPIN_EVERY; + self.guard.repin(); + } + } +} + +impl CollectionHandle for TableHandle +where + K: Sync + Send + 'static + From + Copy + Hash + Eq, +{ + type Key = K; + fn get(&mut self, key: &Self::Key) -> bool { - self.0.pin().get(key).is_some() + self.maybe_repin(); + self.map.with_guard(&self.guard).get(key).is_some() } fn insert(&mut self, key: &Self::Key) -> bool { - self.0.pin().insert(*key, ()).is_some() + self.maybe_repin(); + self.map.with_guard(&self.guard).insert(*key, ()).is_some() } fn remove(&mut self, key: &Self::Key) -> bool { - self.0.pin().remove(key).is_some() + self.maybe_repin(); + self.map.with_guard(&self.guard).remove(key).is_some() } fn update(&mut self, key: &Self::Key) -> bool { - self.0 - .pin() + self.maybe_repin(); + self.map + .with_guard(&self.guard) .compute_if_present(key, |_, _| Some(())) .is_some() }