From bc034215743b7747f8a75cf384cd8bc28859d32a Mon Sep 17 00:00:00 2001 From: Julian Hartl Date: Tue, 9 Jul 2024 17:32:14 +0200 Subject: [PATCH] Use fx hash instead of default hash for index sets --- Cargo.toml | 2 ++ crates/fxindexmap/Cargo.toml | 8 ++++++++ crates/fxindexmap/src/lib.rs | 5 +++++ ir/crates/middle/Cargo.toml | 4 ++-- .../middle/src/analysis/dataflow/backward.rs | 10 ++++------ .../middle/src/analysis/dataflow/forward.rs | 7 +++---- ir/crates/middle/src/cfg/mod.rs | 19 +++++++++---------- 7 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 crates/fxindexmap/Cargo.toml create mode 100644 crates/fxindexmap/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 181221f..83e2182 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,8 @@ members = [ "ir/crates/front", "ir/crates/middle", "lang" +, + "crates/fxindexmap" ] [profile.release] diff --git a/crates/fxindexmap/Cargo.toml b/crates/fxindexmap/Cargo.toml new file mode 100644 index 0000000..584367e --- /dev/null +++ b/crates/fxindexmap/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "fxindexmap" +version = "0.0.0" +edition = "2021" + +[dependencies] +fxhash = "0.2.1" +indexmap = "2.2.6" diff --git a/crates/fxindexmap/src/lib.rs b/crates/fxindexmap/src/lib.rs new file mode 100644 index 0000000..64e688e --- /dev/null +++ b/crates/fxindexmap/src/lib.rs @@ -0,0 +1,5 @@ +use fxhash::FxBuildHasher; +pub use indexmap::*; + +pub type FxIndexMap = IndexMap; +pub type FxIndexSet = IndexSet; diff --git a/ir/crates/middle/Cargo.toml b/ir/crates/middle/Cargo.toml index a014fda..9ac121d 100644 --- a/ir/crates/middle/Cargo.toml +++ b/ir/crates/middle/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [dependencies] tracing = "0.1.40" rustc-hash = "1.1.0" -strum = { version = "0.26.1",features = ["derive"]} +strum = { version = "0.26.1", features = ["derive"] } strum_macros = "0.26.1" tracing-test = "0.2.4" petgraph = "0.6.4" @@ -14,6 +14,6 @@ smallvec = "1.13.1" natrix_front = { path = "../front" } log = "0.4.20" slotmap = "1.0.7" -indexmap = "2.2.6" +fxindexmap = { path = "../../../crates/fxindexmap" } itertools = "0.13.0" derive_more = "0.99.18" diff --git a/ir/crates/middle/src/analysis/dataflow/backward.rs b/ir/crates/middle/src/analysis/dataflow/backward.rs index b8b00f4..9572b40 100644 --- a/ir/crates/middle/src/analysis/dataflow/backward.rs +++ b/ir/crates/middle/src/analysis/dataflow/backward.rs @@ -1,15 +1,13 @@ -use std::collections::VecDeque; - -use indexmap::IndexSet; - use crate::{ analysis::dataflow::{Analysis, DFState, InstrWalker}, cfg::BasicBlockRef, Function, Instr, }; +use fxindexmap::FxIndexSet; +use std::collections::VecDeque; pub struct BackwardAnalysisRunner<'a, A: Analysis> { pub state: DFState, - visited: IndexSet, + visited: FxIndexSet, worklist: VecDeque, pub function: &'a mut Function, _analysis: std::marker::PhantomData, @@ -19,7 +17,7 @@ impl<'a, A: Analysis> BackwardAnalysisRunner<'a, A> { let worklist = function.cfg.dfs_postorder().collect(); Self { worklist, - visited: IndexSet::default(), + visited: FxIndexSet::default(), state: DFState::new(), function, _analysis: std::marker::PhantomData, diff --git a/ir/crates/middle/src/analysis/dataflow/forward.rs b/ir/crates/middle/src/analysis/dataflow/forward.rs index 146281c..d255f81 100644 --- a/ir/crates/middle/src/analysis/dataflow/forward.rs +++ b/ir/crates/middle/src/analysis/dataflow/forward.rs @@ -1,14 +1,13 @@ -use indexmap::IndexSet; - use crate::{ analysis::dataflow::{Analysis, DFState, InstrWalker}, cfg::BasicBlockRef, Function, Instr, }; +use fxindexmap::FxIndexSet; pub struct ForwardAnalysisRunner<'a, A: Analysis> { pub state: DFState, - visited: IndexSet, + visited: FxIndexSet, worklist: Vec, pub function: &'a mut Function, _analysis: std::marker::PhantomData, @@ -18,7 +17,7 @@ impl<'a, A: Analysis> ForwardAnalysisRunner<'a, A> { pub fn new(function: &'a mut Function) -> Self { Self { worklist: vec![function.cfg.entry_block_ref()], - visited: IndexSet::default(), + visited: FxIndexSet::default(), state: DFState::new(), function, _analysis: std::marker::PhantomData, diff --git a/ir/crates/middle/src/cfg/mod.rs b/ir/crates/middle/src/cfg/mod.rs index 0b758c7..1474636 100644 --- a/ir/crates/middle/src/cfg/mod.rs +++ b/ir/crates/middle/src/cfg/mod.rs @@ -2,19 +2,18 @@ use std::fmt::{Debug, Display, Formatter}; +use crate::{ + instruction::{Instr, Op}, + InstrKind, Type, Value, +}; pub use builder::Builder; pub use domtree::DomTree; -use indexmap::IndexSet; +use fxindexmap::FxIndexSet; #[allow(unused_imports)] pub use petgraph::{prelude::*, visit::Walker}; use slotmap::{new_key_type, SlotMap}; use smallvec::SmallVec; -use crate::{ - instruction::{Instr, Op}, - InstrKind, Type, Value, -}; - mod builder; mod domtree; @@ -243,8 +242,8 @@ impl Display for BBArg { #[derive(Debug, Clone)] pub struct BasicBlock { pub id: BasicBlockRef, - pub arguments: IndexSet, - pub instructions: IndexSet, + pub arguments: FxIndexSet, + pub instructions: FxIndexSet, pub terminator: Option, node_index: NodeIndex, pub symbol: String, @@ -254,8 +253,8 @@ impl BasicBlock { pub fn new(id: BasicBlockRef, graph_index: NodeIndex, symbol: String) -> Self { Self { id, - arguments: IndexSet::new(), - instructions: IndexSet::new(), + arguments: FxIndexSet::default(), + instructions: FxIndexSet::default(), symbol, node_index: graph_index, terminator: None,