From b13eb4c81192a57a7e69b10a2d4530ccd91150f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 23 Mar 2023 18:32:47 +0900 Subject: [PATCH] fix(fast-graph): Use fxhash instead of ahash to make iteration order consistent (#7133) **Description:** Some operations of `petgraph` assumes the same iteration order. --- Cargo.lock | 2 +- crates/swc_fast_graph/Cargo.toml | 20 ++++++++++---------- crates/swc_fast_graph/src/digraph.rs | 11 +++++++---- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43417ce096b9..d7a34673f2b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4201,9 +4201,9 @@ dependencies = [ name = "swc_fast_graph" version = "0.17.40" dependencies = [ - "ahash", "indexmap", "petgraph", + "rustc-hash", "swc_common", ] diff --git a/crates/swc_fast_graph/Cargo.toml b/crates/swc_fast_graph/Cargo.toml index 5ea61657e1a1..aa2a26f25b56 100644 --- a/crates/swc_fast_graph/Cargo.toml +++ b/crates/swc_fast_graph/Cargo.toml @@ -1,18 +1,18 @@ [package] -authors = ["강동윤 "] +authors = ["강동윤 "] description = "Faster version of petgraph" -edition = "2021" -include = ["Cargo.toml", "src/**/*.rs"] -license = "Apache-2.0" -name = "swc_fast_graph" -repository = "https://github.com/swc-project/swc.git" -version = "0.17.40" +edition = "2021" +include = ["Cargo.toml", "src/**/*.rs"] +license = "Apache-2.0" +name = "swc_fast_graph" +repository = "https://github.com/swc-project/swc.git" +version = "0.17.40" [lib] bench = false [dependencies] -ahash = "0.7.6" -indexmap = "1.6.1" -petgraph = "0.6" +indexmap = "1.6.1" +petgraph = "0.6" swc_common = { version = "0.29.39", path = "../swc_common" } +rustc-hash = "1.1.0" diff --git a/crates/swc_fast_graph/src/digraph.rs b/crates/swc_fast_graph/src/digraph.rs index f6522acce046..e6922e425b71 100644 --- a/crates/swc_fast_graph/src/digraph.rs +++ b/crates/swc_fast_graph/src/digraph.rs @@ -6,7 +6,7 @@ use std::{ cmp::Ordering, fmt, - hash::{self, Hash}, + hash::{self, BuildHasherDefault, Hash}, iter::{Cloned, DoubleEndedIterator, FromIterator}, marker::PhantomData, ops::Deref, @@ -25,8 +25,11 @@ use petgraph::{ }, Directed, Direction, EdgeType, Incoming, IntoWeightedEdge, Outgoing, Undirected, }; +use rustc_hash::FxHasher; use swc_common::collections::AHashSet; +type FxBuildHasher = BuildHasherDefault; + /// A `GraphMap` with directed edges. /// /// For example, an edge from *1* to *2* is distinct from an edge from *2* to @@ -59,8 +62,8 @@ pub type FastDiGraphMap = FastGraphMap; /// Depends on crate feature `graphmap` (default). #[derive(Clone)] pub struct FastGraphMap { - nodes: IndexMap, ahash::RandomState>, - edges: IndexMap<(N, N), E, ahash::RandomState>, + nodes: IndexMap, FxBuildHasher>, + edges: IndexMap<(N, N), E, FxBuildHasher>, ty: PhantomData, } @@ -584,7 +587,7 @@ where Ty: EdgeType, { from: N, - edges: &'a IndexMap<(N, N), E, ahash::RandomState>, + edges: &'a IndexMap<(N, N), E, FxBuildHasher>, iter: Neighbors<'a, N, Ty>, }