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

Initial rayon support: ParallelExtend + FromParallelIterator #89

Merged
merged 3 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Don't include build output
/target
**/*.rs.bk
# We're a library--don't include our lockfile
Cargo.lock
# Don't include rustfmt backup files
**/*.rs.bk

# Don't include IDE configuration files
.idea
**/.vscode/*
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ sanitize = ['crossbeam-epoch/sanitize']
crossbeam-epoch = "0.8.2"
parking_lot = "0.10"
num_cpus = "1.12.0"
rayon = {version = "1.3", optional = true}
serde = {version = "1.0.105", optional = true}

[dependencies.ahash]
Expand Down
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
displayName: Enable debug symbols
# only --lib --tests b/c of https://github.com/rust-lang/rust/issues/53945
- script: |
env ASAN_OPTIONS="detect_odr_violation=0" RUSTFLAGS="-Z sanitizer=address" cargo test --lib --tests --features sanitize --target x86_64-unknown-linux-gnu
env ASAN_OPTIONS="detect_odr_violation=0" RUSTFLAGS="-Z sanitizer=address" cargo test --lib --tests --all-features --target x86_64-unknown-linux-gnu
displayName: cargo -Z sanitizer=address test
- job: lsan
dependsOn: deny
Expand All @@ -91,7 +91,7 @@ jobs:
cat Cargo.toml
displayName: Enable debug symbols
- script: |
env RUSTFLAGS="-Z sanitizer=leak" cargo test --features sanitize --target x86_64-unknown-linux-gnu
env RUSTFLAGS="-Z sanitizer=leak" cargo test --all-features --target x86_64-unknown-linux-gnu
displayName: cargo -Z sanitizer=leak test
- template: coverage.yml@templates
parameters:
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ mod raw;
mod set;
mod set_ref;

#[cfg(feature = "rayon")]
mod rayon_impls;

#[cfg(feature = "serde")]
mod serde_impls;

Expand Down
117 changes: 117 additions & 0 deletions src/rayon_impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use crate::HashMap;
use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator};
use std::hash::{BuildHasher, Hash};

impl<K, V, S> ParallelExtend<(K, V)> for HashMap<K, V, S>
jonhoo marked this conversation as resolved.
Show resolved Hide resolved
where
K: Clone + Hash + Ord + Send + Sync + 'static,
V: Send + Sync + 'static,
S: BuildHasher + Sync,
{
fn par_extend<I>(&mut self, par_iter: I)
Others marked this conversation as resolved.
Show resolved Hide resolved
where
I: IntoParallelIterator<Item = (K, V)>,
{
(&*self).par_extend(par_iter);
}
}

impl<K, V, S> ParallelExtend<(K, V)> for &HashMap<K, V, S>
where
K: Clone + Hash + Ord + Send + Sync + 'static,
V: Send + Sync + 'static,
S: BuildHasher + Sync,
{
fn par_extend<I>(&mut self, par_iter: I)
where
I: IntoParallelIterator<Item = (K, V)>,
{
par_iter.into_par_iter().for_each_init(
|| self.guard(),
|guard, (k, v)| {
self.insert(k, v, &guard);
},
);
}
}

impl<K, V, S> FromParallelIterator<(K, V)> for HashMap<K, V, S>
where
K: Clone + Hash + Ord + Send + Sync + 'static,
V: Send + Sync + 'static,
S: BuildHasher + Default + Sync,
{
fn from_par_iter<I>(par_iter: I) -> Self
where
I: IntoParallelIterator<Item = (K, V)>,
{
let mut created_map = HashMap::with_hasher(S::default());
created_map.par_extend(par_iter);
created_map
}
}

#[cfg(test)]
mod test {
use crate::HashMap;
use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelExtend};

#[test]
fn parallel_extend_by_nothing() {
let to_extend_with = Vec::new();

let mut map = HashMap::new();
let guard = map.guard();
map.insert(1, 2, &guard);
map.insert(3, 4, &guard);

map.par_extend(to_extend_with.into_par_iter());

assert_eq!(map.len(), 2);

assert_eq!(map.get(&1, &guard), Some(&2));
assert_eq!(map.get(&3, &guard), Some(&4));
}

#[test]
fn parallel_extend_by_a_bunch() {
let mut to_extend_with = Vec::new();
for i in 0..100 {
to_extend_with.push((i + 100, i * 10));
}

let mut map = HashMap::new();
let guard = map.guard();
map.insert(1, 2, &guard);
map.insert(3, 4, &guard);

map.par_extend(to_extend_with.into_par_iter());
assert_eq!(map.len(), 102);

assert_eq!(map.get(&1, &guard), Some(&2));
assert_eq!(map.get(&3, &guard), Some(&4));
assert_eq!(map.get(&100, &guard), Some(&0));
assert_eq!(map.get(&199, &guard), Some(&990));
}

#[test]
fn from_empty_parallel_iter() {
let to_create_from: Vec<(i32, i32)> = Vec::new();
let created_map: HashMap<i32, i32> = HashMap::from_par_iter(to_create_from.into_par_iter());
assert_eq!(created_map.len(), 0);
}

#[test]
fn from_large_parallel_iter() {
let mut to_create_from: Vec<(i32, i32)> = Vec::new();
for i in 0..100 {
to_create_from.push((i + 100, i * 10));
}
let created_map: HashMap<i32, i32> = HashMap::from_par_iter(to_create_from.into_par_iter());
assert_eq!(created_map.len(), 100);

let guard = created_map.guard();
assert_eq!(created_map.get(&100, &guard), Some(&0));
assert_eq!(created_map.get(&199, &guard), Some(&990));
}
}