diff --git a/src/extract.rs b/src/extract.rs index 74fbdc60..b2ca9b16 100644 --- a/src/extract.rs +++ b/src/extract.rs @@ -1,7 +1,7 @@ use std::cmp::Ordering; use std::fmt::Debug; -use crate::util::HashMap; +use crate::util::{hashmap_with_capacity, HashMap}; use crate::{Analysis, EClass, EGraph, Id, Language, RecExpr}; /** Extracting a single [`RecExpr`] from an [`EGraph`]. @@ -134,8 +134,9 @@ pub trait CostFunction { /// down the [`RecExpr`]. /// fn cost_rec(&mut self, expr: &RecExpr) -> Self::Cost { - let mut costs: HashMap = HashMap::default(); - for (i, node) in expr.as_ref().iter().enumerate() { + let nodes = expr.as_ref(); + let mut costs = hashmap_with_capacity::(nodes.len()); + for (i, node) in nodes.iter().enumerate() { let cost = self.cost(node, |i| costs[&i].clone()); costs.insert(Id::from(i), cost); } diff --git a/src/language.rs b/src/language.rs index 23616387..6878669f 100644 --- a/src/language.rs +++ b/src/language.rs @@ -430,7 +430,7 @@ impl RecExpr { } pub(crate) fn compact(mut self) -> Self { - let mut ids = HashMap::::default(); + let mut ids = hashmap_with_capacity::(self.nodes.len()); let mut set = IndexSet::default(); for (i, node) in self.nodes.drain(..).enumerate() { let node = node.map_children(|id| ids[&id]); diff --git a/src/util.rs b/src/util.rs index c5a20200..9144e539 100644 --- a/src/util.rs +++ b/src/util.rs @@ -61,6 +61,10 @@ mod hashmap { pub(crate) type HashSet = hashbrown::HashSet; } +pub(crate) fn hashmap_with_capacity(cap: usize) -> hashmap::HashMap { + hashmap::HashMap::with_capacity_and_hasher(cap, <_>::default()) +} + pub(crate) type IndexMap = indexmap::IndexMap; pub(crate) type IndexSet = indexmap::IndexSet;