From 8593ad58b869d11b8fb911ac98e2b6d82cc6e071 Mon Sep 17 00:00:00 2001 From: oflatt Date: Mon, 28 Oct 2024 15:04:27 -0700 Subject: [PATCH] refactor util hashmap --- src/extract.rs | 8 +++----- src/gj.rs | 25 +++++++++---------------- src/typechecking.rs | 9 ++++----- src/util.rs | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/extract.rs b/src/extract.rs index e895082b..230d6dc2 100644 --- a/src/extract.rs +++ b/src/extract.rs @@ -1,9 +1,7 @@ -use hashbrown::hash_map::Entry; - use crate::ast::Symbol; use crate::termdag::{Term, TermDag}; use crate::util::HashMap; -use crate::{ArcSort, EGraph, Function, Id, Value}; +use crate::{ArcSort, EGraph, Function, HEntry, Id, Value}; pub type Cost = usize; @@ -188,11 +186,11 @@ impl<'a> Extractor<'a> { let id = self.find_id(output.value); match self.costs.entry(id) { - Entry::Vacant(e) => { + HEntry::Vacant(e) => { did_something = true; e.insert(make_new_pair()); } - Entry::Occupied(mut e) => { + HEntry::Occupied(mut e) => { if new_cost < e.get().0 { did_something = true; e.insert(make_new_pair()); diff --git a/src/gj.rs b/src/gj.rs index 940ec037..be05b330 100644 --- a/src/gj.rs +++ b/src/gj.rs @@ -1,6 +1,7 @@ use indexmap::map::Entry; use log::log_enabled; use smallvec::SmallVec; +use util::HashMap; use crate::{ core::{Atom, AtomTerm, ResolvedAtomTerm, ResolvedCall}, @@ -792,24 +793,16 @@ impl Debug for LazyTrie { } } -#[cfg(feature = "nondeterministic")] -type SparseMap = HashMap; -#[cfg(feature = "nondeterministic")] -type SEntry<'a, A, B, D> = hashbrown::hash_map::Entry<'a, A, B, D>; -#[cfg(not(feature = "nondeterministic"))] -type SparseMap = IndexMap; -#[cfg(not(feature = "nondeterministic"))] -type SEntry<'a, A, B> = Entry<'a, A, B>; type RowIdx = u32; #[derive(Debug)] enum LazyTrieInner { Borrowed { index: Rc, - map: SparseMap, + map: HashMap, }, Delayed(SmallVec<[RowIdx; 4]>), - Sparse(SparseMap), + Sparse(HashMap), } impl Default for LazyTrie { @@ -857,7 +850,7 @@ impl LazyTrie { let this = unsafe { &mut *self.0.get() }; match this { LazyTrieInner::Borrowed { index, .. } => { - let mut map = SparseMap::with_capacity_and_hasher(index.len(), Default::default()); + let mut map = HashMap::with_capacity_and_hasher(index.len(), Default::default()); map.extend(index.iter().filter_map(|(v, ixs)| { LazyTrie::from_indexes(access.filter_live(ixs)).map(|trie| (v, trie)) })); @@ -895,8 +888,8 @@ impl LazyTrie { LazyTrieInner::Borrowed { index, map } => { let ixs = index.get(&value)?; match map.entry(value) { - SEntry::Occupied(o) => Some(o.into_mut()), - SEntry::Vacant(v) => { + HEntry::Occupied(o) => Some(o.into_mut()), + HEntry::Vacant(v) => { Some(v.insert(LazyTrie::from_indexes(access.filter_live(ixs))?)) } } @@ -942,20 +935,20 @@ impl<'a> TrieAccess<'a> { #[cold] fn make_trie_inner(&self, idxs: &[RowIdx]) -> LazyTrieInner { let arity = self.function.schema.input.len(); - let mut map = SparseMap::default(); + let mut map: HashMap = HashMap::default(); let mut insert = |i: usize, tup: &[Value], out: &TupleOutput, val: Value| { if self.timestamp_range.contains(&out.timestamp) && self.constraints.iter().all(|c| c.check(tup, out)) { match map.entry(val) { - SEntry::Occupied(mut e) => { + HEntry::Occupied(mut e) => { if let LazyTrieInner::Delayed(ref mut v) = e.get_mut().0.get_mut() { v.push(i as RowIdx) } else { unreachable!() } } - SEntry::Vacant(e) => { + HEntry::Vacant(e) => { e.insert(LazyTrie(UnsafeCell::new(LazyTrieInner::Delayed( smallvec::smallvec![i as RowIdx,], )))); diff --git a/src/typechecking.rs b/src/typechecking.rs index 80f0e464..71e710bc 100644 --- a/src/typechecking.rs +++ b/src/typechecking.rs @@ -1,6 +1,5 @@ use crate::{core::CoreRule, *}; use ast::Rule; -use hashbrown::hash_map::Entry; #[derive(Clone, Debug)] pub struct FuncType { @@ -63,8 +62,8 @@ impl TypeInfo { pub fn add_presort(&mut self, span: Span) -> Result<(), TypeError> { let name = S::presort_name(); match self.presorts.entry(name) { - Entry::Occupied(_) => Err(TypeError::SortAlreadyBound(name, span)), - Entry::Vacant(e) => { + HEntry::Occupied(_) => Err(TypeError::SortAlreadyBound(name, span)), + HEntry::Vacant(e) => { e.insert(S::make_sort); self.reserved_primitives.extend(S::reserved_primitives()); Ok(()) @@ -76,8 +75,8 @@ impl TypeInfo { let name = sort.name(); match self.sorts.entry(name) { - Entry::Occupied(_) => Err(TypeError::SortAlreadyBound(name, span)), - Entry::Vacant(e) => { + HEntry::Occupied(_) => Err(TypeError::SortAlreadyBound(name, span)), + HEntry::Vacant(e) => { e.insert(sort.clone()); sort.register_primitives(self); Ok(()) diff --git a/src/util.rs b/src/util.rs index 47191c23..ae44dc8b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -8,9 +8,24 @@ use crate::*; pub(crate) type BuildHasher = std::hash::BuildHasherDefault; +/// Use an index map by default everywhere. +/// We could fix the seed, but symbol generation is not determinisic so +/// this doesn't fix the problem. +#[cfg(not(feature = "nondeterministic"))] +pub(crate) type HashMap = indexmap::IndexMap; +#[cfg(feature = "nondeterministic")] pub(crate) type HashMap = hashbrown::HashMap; + +#[cfg(not(feature = "nondeterministic"))] +pub(crate) type HashSet = indexmap::IndexSet; +#[cfg(feature = "nondeterministic")] pub(crate) type HashSet = hashbrown::HashSet; +#[cfg(feature = "nondeterministic")] +pub(crate) type HEntry<'a, A, B, D> = hashbrown::hash_map::Entry<'a, A, B, D>; +#[cfg(not(feature = "nondeterministic"))] +pub(crate) type HEntry<'a, A, B> = Entry<'a, A, B>; + pub type IndexMap = indexmap::IndexMap; pub type IndexSet = indexmap::IndexSet;