fix(fast-graph): Use fxhash instead of ahash to make iteration order consistent (#7133)

**Description:**

Some operations of `petgraph` assumes the same iteration order.
This commit is contained in:
Donny/강동윤 2023-03-23 18:32:47 +09:00 committed by GitHub
parent b178ff5661
commit b13eb4c811
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 15 deletions

2
Cargo.lock generated
View File

@ -4201,9 +4201,9 @@ dependencies = [
name = "swc_fast_graph" name = "swc_fast_graph"
version = "0.17.40" version = "0.17.40"
dependencies = [ dependencies = [
"ahash",
"indexmap", "indexmap",
"petgraph", "petgraph",
"rustc-hash",
"swc_common", "swc_common",
] ]

View File

@ -1,18 +1,18 @@
[package] [package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"] authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Faster version of petgraph" description = "Faster version of petgraph"
edition = "2021" edition = "2021"
include = ["Cargo.toml", "src/**/*.rs"] include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0" license = "Apache-2.0"
name = "swc_fast_graph" name = "swc_fast_graph"
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"
version = "0.17.40" version = "0.17.40"
[lib] [lib]
bench = false bench = false
[dependencies] [dependencies]
ahash = "0.7.6" indexmap = "1.6.1"
indexmap = "1.6.1" petgraph = "0.6"
petgraph = "0.6"
swc_common = { version = "0.29.39", path = "../swc_common" } swc_common = { version = "0.29.39", path = "../swc_common" }
rustc-hash = "1.1.0"

View File

@ -6,7 +6,7 @@
use std::{ use std::{
cmp::Ordering, cmp::Ordering,
fmt, fmt,
hash::{self, Hash}, hash::{self, BuildHasherDefault, Hash},
iter::{Cloned, DoubleEndedIterator, FromIterator}, iter::{Cloned, DoubleEndedIterator, FromIterator},
marker::PhantomData, marker::PhantomData,
ops::Deref, ops::Deref,
@ -25,8 +25,11 @@ use petgraph::{
}, },
Directed, Direction, EdgeType, Incoming, IntoWeightedEdge, Outgoing, Undirected, Directed, Direction, EdgeType, Incoming, IntoWeightedEdge, Outgoing, Undirected,
}; };
use rustc_hash::FxHasher;
use swc_common::collections::AHashSet; use swc_common::collections::AHashSet;
type FxBuildHasher = BuildHasherDefault<FxHasher>;
/// A `GraphMap` with directed edges. /// A `GraphMap` with directed edges.
/// ///
/// For example, an edge from *1* to *2* is distinct from an edge from *2* to /// For example, an edge from *1* to *2* is distinct from an edge from *2* to
@ -59,8 +62,8 @@ pub type FastDiGraphMap<N, E> = FastGraphMap<N, E, Directed>;
/// Depends on crate feature `graphmap` (default). /// Depends on crate feature `graphmap` (default).
#[derive(Clone)] #[derive(Clone)]
pub struct FastGraphMap<N, E, Ty> { pub struct FastGraphMap<N, E, Ty> {
nodes: IndexMap<N, Vec<(N, CompactDirection)>, ahash::RandomState>, nodes: IndexMap<N, Vec<(N, CompactDirection)>, FxBuildHasher>,
edges: IndexMap<(N, N), E, ahash::RandomState>, edges: IndexMap<(N, N), E, FxBuildHasher>,
ty: PhantomData<Ty>, ty: PhantomData<Ty>,
} }
@ -584,7 +587,7 @@ where
Ty: EdgeType, Ty: EdgeType,
{ {
from: N, from: N,
edges: &'a IndexMap<(N, N), E, ahash::RandomState>, edges: &'a IndexMap<(N, N), E, FxBuildHasher>,
iter: Neighbors<'a, N, Ty>, iter: Neighbors<'a, N, Ty>,
} }