Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SccIndexTable #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/adapters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub use self::{
btreemap::ParkingLotRwLockBTreeMapTable, btreemap::StdRwLockBTreeMapTable,
chashmap::CHashMapTable, contrie::ContrieTable, crossbeam_skiplist::CrossbeamSkipMapTable,
dashmap::DashMapTable, evmap::EvmapTable, flurry::FlurryTable, scc::SccMapTable,
std::ParkingLotRwLockStdHashMapTable, std::StdRwLockStdHashMapTable,
dashmap::DashMapTable, evmap::EvmapTable, flurry::FlurryTable, scc::SccIndexTable,
scc::SccMapTable, std::ParkingLotRwLockStdHashMapTable, std::StdRwLockStdHashMapTable,
};

mod btreemap;
Expand Down
82 changes: 65 additions & 17 deletions src/adapters/scc.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
use bustle::*;
use std::hash::{BuildHasher, Hash};
use std::sync::Arc;

use super::Value;
use bustle::*;
use scc::hash_map::{Entry, HashMap};

#[derive(Clone)]
pub struct SccMapTable<K, H>(Arc<HashMap<K, Value, H>>)
where
K: Eq + Hash,
H: BuildHasher;
pub struct SccMapTable<K: Eq + Hash + Sync + 'static, H: BuildHasher + 'static>(
Arc<scc::HashMap<K, Value, H>>,
);

impl<K, H> Collection for SccMapTable<K, H>
where
K: Send + Sync + From<u64> + Copy + 'static + Hash + Eq + std::fmt::Debug,
H: BuildHasher + Default + Send + Sync + 'static + Clone,
K: Send + Sync + From<u64> + Copy + Hash + Ord + 'static,
H: BuildHasher + Default + Send + Sync + Clone + 'static,
{
type Handle = Self;

fn with_capacity(capacity: usize) -> Self {
Self(Arc::new(HashMap::with_capacity_and_hasher(
Self(Arc::new(scc::HashMap::with_capacity_and_hasher(
capacity,
H::default(),
)))
Expand All @@ -32,13 +30,13 @@ where

impl<K, H> CollectionHandle for SccMapTable<K, H>
where
K: Send + Sync + From<u64> + Copy + 'static + Hash + Eq + std::fmt::Debug,
H: BuildHasher + Default + Send + Sync + 'static + Clone,
K: Send + Sync + From<u64> + Copy + Hash + Ord + 'static,
H: BuildHasher + Default + Send + Sync + Clone + 'static,
{
type Key = K;

fn get(&mut self, key: &Self::Key) -> bool {
self.0.read(key, |_, v| *v).is_some()
self.0.read(key, |_, _| ()).is_some()
}

fn insert(&mut self, key: &Self::Key) -> bool {
Expand All @@ -50,12 +48,62 @@ where
}

fn update(&mut self, key: &Self::Key) -> bool {
match self.0.entry(*key) {
Entry::Occupied(mut v) => {
*v.get_mut() += 1;
true
self.0.update(key, |_, v| *v += 1).is_some()
}
}

#[derive(Clone)]
pub struct SccIndexTable<K: Clone + Eq + Hash + Sync + 'static, H: BuildHasher + 'static>(
Arc<scc::HashIndex<K, Value, H>>,
);

impl<K, H> Collection for SccIndexTable<K, H>
where
K: Send + Sync + From<u64> + Copy + Hash + Ord + 'static,
H: BuildHasher + Default + Send + Sync + Clone + 'static,
{
type Handle = Self;

fn with_capacity(capacity: usize) -> Self {
Self(Arc::new(scc::HashIndex::with_capacity_and_hasher(
capacity,
H::default(),
)))
}

fn pin(&self) -> Self::Handle {
self.clone()
}
}

impl<K, H> CollectionHandle for SccIndexTable<K, H>
where
K: Send + Sync + From<u64> + Copy + Hash + Ord + 'static,
H: BuildHasher + Default + Send + Sync + Clone + 'static,
{
type Key = K;

fn get(&mut self, key: &Self::Key) -> bool {
self.0.peek_with(key, |_, _| ()).is_some()
}

fn insert(&mut self, key: &Self::Key) -> bool {
self.0.insert(*key, 0).is_ok()
}

fn remove(&mut self, key: &Self::Key) -> bool {
self.0.remove(key)
}

fn update(&mut self, key: &Self::Key) -> bool {
if let scc::hash_index::Entry::Occupied(mut o) = self.0.entry(*key) {
unsafe {
let val = o.get_mut();
*val += 1;
}
Entry::Vacant(_) => false,
true
} else {
false
}
}
}
1 change: 1 addition & 0 deletions src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ where
case::<EvmapTable<u64, H>>("Evmap", options, h);
case::<ContrieTable<u64, H>>("Contrie", options, h);
case::<SccMapTable<u64, H>>("SccMap", options, h);
case::<SccMapTable<u64, H>>("SccIndex", options, h);
}

pub fn bench(options: &Options) {
Expand Down