Skip to content

Commit

Permalink
Allow alternative hash functions in GraphMap
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-levin committed Mar 26, 2024
1 parent e219ecf commit 0353923
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 66 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ defmac = "0.2.1"
itertools = { version = "0.11.0", default-features = false }
odds = { version = "0.4.0" }
rand = "0.5.5"
ahash = "0.7.2"
fxhash = "0.2.1"

[features]
rayon = ["dep:rayon", "indexmap/rayon"]
Expand Down
41 changes: 26 additions & 15 deletions benches/graphmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate test;

use petgraph::prelude::*;
use rayon::iter::ParallelIterator;
use std::hash::BuildHasher;
use test::Bencher;

#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand All @@ -28,8 +29,10 @@ fn test_nodes() -> Vec<MyStruct> {
nodes
}

fn test_graph(data: &Vec<MyStruct>) -> DiGraphMap<&MyStruct, usize> {
let mut gr: DiGraphMap<&MyStruct, usize> = DiGraphMap::new();
fn test_graph<H: BuildHasher + Default>(
data: &Vec<MyStruct>,
) -> GraphMap<&MyStruct, usize, Directed, H> {
let mut gr = GraphMap::new();

for i in 0..2500 {
gr.add_node(&data[i]);
Expand All @@ -44,26 +47,34 @@ fn test_graph(data: &Vec<MyStruct>) -> DiGraphMap<&MyStruct, usize> {
gr
}

#[bench]
fn graphmap_serial_bench(bench: &mut Bencher) {
let data = test_nodes();
let gr = test_graph(&data);
bench.iter(|| {
let mut sources = vec![];
for n in gr.nodes() {
for (src, _, e) in gr.edges_directed(n, Direction::Outgoing) {
if *e == 500 {
sources.push(src.clone());
macro_rules! test_case_with_hasher {
($name:ident, $hasher:path) => {
#[bench]
fn $name(bench: &mut Bencher) {
let data = test_nodes();
let gr = test_graph::<$hasher>(&data);
bench.iter(|| {
let mut sources = vec![];
for n in gr.nodes() {
for (src, _, e) in gr.edges_directed(n, Direction::Outgoing) {
if *e == 500 {
sources.push(src.clone());
}
}
}
}
});
}
});
};
}

test_case_with_hasher!(graphmap_serial_bench, std::hash::RandomState);
test_case_with_hasher!(graphmap_serial_bench_fxhash, fxhash::FxBuildHasher);
test_case_with_hasher!(graphmap_serial_bench_ahash, ahash::RandomState);

#[bench]
fn graphmap_parallel_bench(bench: &mut Bencher) {
let data = test_nodes();
let gr = test_graph(&data);
let gr = test_graph::<std::hash::RandomState>(&data);
bench.iter(|| {
let sources: Vec<MyStruct> = gr
.par_nodes()
Expand Down

0 comments on commit 0353923

Please sign in to comment.