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

Swap ahash with foldhash #1294

Open
wants to merge 4 commits into
base: main
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
11 changes: 8 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repository = "https://github.com/Qiskit/rustworkx"
license = "Apache-2.0"

[workspace.dependencies]
ahash = "0.8.6"
foldhash = "0.1.2"
fixedbitset = "0.4.2"
hashbrown = { version = ">=0.13, <0.15", features = ["rayon"] }
indexmap = { version = ">=1.9, <3", features = ["rayon"] }
Expand All @@ -41,7 +41,7 @@ name = "rustworkx"
crate-type = ["cdylib"]

[dependencies]
ahash.workspace = true
foldhash.workspace = true
fixedbitset.workspace = true
hashbrown.workspace = true
indexmap.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion rustworkx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository.workspace = true
license.workspace = true

[dependencies]
ahash.workspace = true
foldhash.workspace = true
fixedbitset.workspace = true
hashbrown.workspace = true
indexmap.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion rustworkx-core/src/coloring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// License for the specific language governing permissions and limitations
// under the License.

use ahash::RandomState;
use foldhash::fast::RandomState;
use priority_queue::PriorityQueue;
use std::cmp::Ordering;
use std::cmp::Reverse;
Expand Down
2 changes: 1 addition & 1 deletion rustworkx-core/src/connectivity/min_cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
F: FnMut(G::EdgeRef) -> K,
K: Copy + Ord + Zero + AddAssign,
{
let mut pq = PriorityQueue::<G::NodeId, K, ahash::RandomState>::from(
let mut pq = PriorityQueue::<G::NodeId, K, foldhash::fast::RandomState>::from(
graph
.node_identifiers()
.map(|nx| (nx, K::zero()))
Expand Down
14 changes: 7 additions & 7 deletions rustworkx-core/src/dictmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
// under the License.

//! This module contains the [`DictMap`] type alias which is a combination of
//! [`IndexMap`] and [`AHash`].
//! [`IndexMap`] and [`Foldhash`].
//!
//! It is used as a return type for rustworkx for compatibility
//! with Python's dict which preserves insertion order.
//!
//! [`AHash`]: https://crates.io/crates/ahash
//! [`Foldhash`]: https://crates.io/crates/foldhash

use indexmap::IndexMap;

/// Convenient alias to build an [`IndexMap`] using a custom hasher.
/// For the moment, we use ahash which is the default hasher
/// For the moment, we use foldhash which is the default hasher
/// for [`HashMap`], another hashmap we use.
///
/// [`HashMap`]: https://docs.rs/hashbrown/0.11.2/hashbrown/hash_map/struct.HashMap.html
pub type DictMap<K, V> = IndexMap<K, V, ahash::RandomState>;
/// [`HashMap`]: https://docs.rs/hashbrown/0.15.0/hashbrown/hash_map/struct.HashMap.html
pub type DictMap<K, V> = IndexMap<K, V, foldhash::fast::RandomState>;

pub trait InitWithHasher {
fn new() -> Self
Expand All @@ -40,11 +40,11 @@ pub trait InitWithHasher {
impl<K, V> InitWithHasher for DictMap<K, V> {
#[inline]
fn new() -> Self {
indexmap::IndexMap::with_capacity_and_hasher(0, ahash::RandomState::default())
indexmap::IndexMap::with_capacity_and_hasher(0, foldhash::fast::RandomState::default())
}

#[inline]
fn with_capacity(n: usize) -> Self {
indexmap::IndexMap::with_capacity_and_hasher(n, ahash::RandomState::default())
indexmap::IndexMap::with_capacity_and_hasher(n, foldhash::fast::RandomState::default())
}
}
6 changes: 3 additions & 3 deletions rustworkx-core/src/graph_ext/contraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ where

fn contract_stable<G, F, E: Error>(
graph: &mut G,
mut nodes: IndexSet<G::NodeId, ahash::RandomState>,
mut nodes: IndexSet<G::NodeId, foldhash::fast::RandomState>,
weight: G::NodeWeight,
weight_combo_fn: Option<F>,
) -> Result<G::NodeId, E>
Expand Down Expand Up @@ -499,7 +499,7 @@ where
Ok(node_index)
}

fn can_contract<G>(graph: G, nodes: &IndexSet<G::NodeId, ahash::RandomState>) -> bool
fn can_contract<G>(graph: G, nodes: &IndexSet<G::NodeId, foldhash::fast::RandomState>) -> bool
where
G: Data + Visitable + IntoEdgesDirected,
G::NodeId: Eq + Hash,
Expand Down Expand Up @@ -536,7 +536,7 @@ type NoCallback<E> = Option<fn(&E, &E) -> Result<E, Infallible>>;
fn add_edges<G, F, E>(
graph: &mut G,
new_node: G::NodeId,
nodes: &IndexSet<G::NodeId, ahash::RandomState>,
nodes: &IndexSet<G::NodeId, foldhash::fast::RandomState>,
mut weight_combo_fn: Option<F>,
) -> Result<(), E>
where
Expand Down
2 changes: 1 addition & 1 deletion rustworkx-core/src/shortest_path/all_shortest_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::dictmap::*;
/// use rustworkx_core::dictmap::DictMap;
/// use rustworkx_core::shortest_path::all_shortest_paths;
/// use rustworkx_core::Result;
/// use ahash::HashSet;
/// use foldhash::HashSet;
///
/// let mut graph : Graph<(), (), Directed>= Graph::new();
/// let a = graph.add_node(()); // node with no weight
Expand Down
2 changes: 1 addition & 1 deletion rustworkx-core/tests/graph_ext/contraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// License for the specific language governing permissions and limitations
// under the License.

use ahash::HashSet;
use foldhash::HashSet;
use hashbrown::HashMap;
use petgraph::data::Build;
use petgraph::visit::{
Expand Down
10 changes: 5 additions & 5 deletions src/connectivity/johnson_simple_cycles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// License for the specific language governing permissions and limitations
// under the License.

use ahash::RandomState;
use foldhash::fast::RandomState;
use hashbrown::{HashMap, HashSet};
use indexmap::IndexSet;

Expand Down Expand Up @@ -141,7 +141,7 @@ fn unblock(
#[allow(clippy::too_many_arguments)]
fn process_stack(
start_node: NodeIndex,
stack: &mut Vec<(NodeIndex, IndexSet<NodeIndex, ahash::RandomState>)>,
stack: &mut Vec<(NodeIndex, IndexSet<NodeIndex, foldhash::fast::RandomState>)>,
path: &mut Vec<NodeIndex>,
closed: &mut HashSet<NodeIndex>,
blocked: &mut HashSet<NodeIndex>,
Expand All @@ -165,7 +165,7 @@ fn process_stack(
next_node,
subgraph
.neighbors(next_node)
.collect::<IndexSet<NodeIndex, ahash::RandomState>>(),
.collect::<IndexSet<NodeIndex, foldhash::fast::RandomState>>(),
));
closed.remove(&next_node);
blocked.insert(next_node);
Expand Down Expand Up @@ -206,7 +206,7 @@ impl SimpleCycleIter {
}));
}
// Restore previous state if it exists
let mut stack: Vec<(NodeIndex, IndexSet<NodeIndex, ahash::RandomState>)> =
let mut stack: Vec<(NodeIndex, IndexSet<NodeIndex, foldhash::fast::RandomState>)> =
std::mem::take(&mut slf.stack);
let mut path: Vec<NodeIndex> = std::mem::take(&mut slf.path);
let mut closed: HashSet<NodeIndex> = std::mem::take(&mut slf.closed);
Expand Down Expand Up @@ -268,7 +268,7 @@ impl SimpleCycleIter {
slf.start_node,
subgraph
.neighbors(slf.start_node)
.collect::<IndexSet<NodeIndex, ahash::RandomState>>(),
.collect::<IndexSet<NodeIndex, foldhash::fast::RandomState>>(),
)];
if let Some(res) = process_stack(
slf.start_node,
Expand Down
Loading