From a67bb29a36cf4c063f82a93a27f050ba3ba873e5 Mon Sep 17 00:00:00 2001 From: Folkert Date: Tue, 22 Mar 2022 21:58:49 +0100 Subject: [PATCH] remove hashmap --- ast/src/lang/env.rs | 4 +-- ast/src/lang/scope.rs | 2 +- compiler/can/src/env.rs | 4 +-- compiler/can/src/scope.rs | 2 +- compiler/module/src/symbol.rs | 52 ++++++++++++----------------------- 5 files changed, 24 insertions(+), 40 deletions(-) diff --git a/ast/src/lang/env.rs b/ast/src/lang/env.rs index 1b04f417d2..7e7f5a1670 100644 --- a/ast/src/lang/env.rs +++ b/ast/src/lang/env.rs @@ -117,7 +117,7 @@ impl<'a> Env<'a> { if module_id == self.home { match self.ident_ids.get_id(&ident) { Some(ident_id) => { - let symbol = Symbol::new(module_id, *ident_id); + let symbol = Symbol::new(module_id, ident_id); self.qualified_lookups.insert(symbol); @@ -138,7 +138,7 @@ impl<'a> Env<'a> { match self.dep_idents.get(&module_id) { Some(exposed_ids) => match exposed_ids.get_id(&ident) { Some(ident_id) => { - let symbol = Symbol::new(module_id, *ident_id); + let symbol = Symbol::new(module_id, ident_id); self.qualified_lookups.insert(symbol); diff --git a/ast/src/lang/scope.rs b/ast/src/lang/scope.rs index 2bb1f838aa..b01c818064 100644 --- a/ast/src/lang/scope.rs +++ b/ast/src/lang/scope.rs @@ -244,7 +244,7 @@ impl Scope { // when the value was exposed in the module header, // use that existing IdentId. Otherwise, create a fresh one. let ident_id = match exposed_ident_ids.get_id(&ident) { - Some(ident_id) => *ident_id, + Some(ident_id) => ident_id, None => all_ident_ids.add(ident.clone().into()), }; diff --git a/compiler/can/src/env.rs b/compiler/can/src/env.rs index 667817e11c..ebbb79b349 100644 --- a/compiler/can/src/env.rs +++ b/compiler/can/src/env.rs @@ -87,7 +87,7 @@ impl<'a> Env<'a> { if module_id == self.home { match self.ident_ids.get_id(&ident) { Some(ident_id) => { - let symbol = Symbol::new(module_id, *ident_id); + let symbol = Symbol::new(module_id, ident_id); if is_type_name { self.qualified_type_lookups.insert(symbol); @@ -112,7 +112,7 @@ impl<'a> Env<'a> { match self.dep_idents.get(&module_id) { Some(exposed_ids) => match exposed_ids.get_id(&ident) { Some(ident_id) => { - let symbol = Symbol::new(module_id, *ident_id); + let symbol = Symbol::new(module_id, ident_id); if is_type_name { self.qualified_type_lookups.insert(symbol); diff --git a/compiler/can/src/scope.rs b/compiler/can/src/scope.rs index 76ac02ea51..34e6369fec 100644 --- a/compiler/can/src/scope.rs +++ b/compiler/can/src/scope.rs @@ -203,7 +203,7 @@ impl Scope { // when the value was exposed in the module header, // use that existing IdentId. Otherwise, create a fresh one. let ident_id = match exposed_ident_ids.get_id(&ident) { - Some(ident_id) => *ident_id, + Some(ident_id) => ident_id, None => all_ident_ids.add(ident.clone()), }; diff --git a/compiler/module/src/symbol.rs b/compiler/module/src/symbol.rs index 46b5f25be5..f72357ee35 100644 --- a/compiler/module/src/symbol.rs +++ b/compiler/module/src/symbol.rs @@ -236,7 +236,7 @@ impl Interns { match self.all_ident_ids.get(&module_id) { Some(ident_ids) => match ident_ids.get_id(&ident) { - Some(ident_id) => Symbol::new(module_id, *ident_id), + Some(ident_id) => Symbol::new(module_id, ident_id), None => { panic!("Interns::symbol could not find ident entry for {:?} for module {:?} in Interns {:?}", ident, module_id, self); } @@ -535,8 +535,6 @@ pub struct IdentId(u32); /// Since these are interned strings, this shouldn't result in many total allocations in practice. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct IdentIds { - by_ident: MutMap, - /// Each IdentId is an index into this Vec by_id: Vec, @@ -555,24 +553,18 @@ impl IdentIds { let by_id = &mut self.by_id; let ident_id = IdentId(by_id.len() as u32); - self.by_ident.insert(ident_name.clone(), ident_id); by_id.push(ident_name); ident_id } pub fn get_or_insert(&mut self, name: &Ident) -> IdentId { - use std::collections::hash_map::Entry; + match self.get_id(name) { + Some(id) => id, + None => { + let ident_id = IdentId(self.by_id.len() as u32); - match self.by_ident.entry(name.clone()) { - Entry::Occupied(occupied) => *occupied.get(), - Entry::Vacant(vacant) => { - let by_id = &mut self.by_id; - let ident_id = IdentId(by_id.len() as u32); - - by_id.push(name.clone()); - - vacant.insert(ident_id); + self.by_id.push(name.clone()); ident_id } @@ -588,14 +580,11 @@ impl IdentIds { ) -> Result { let old_ident: Ident = old_ident_name.into(); - let ident_id_ref_opt = self.by_ident.get(&old_ident); + let ident_id_ref_opt = self.get_id(&old_ident); match ident_id_ref_opt { Some(ident_id_ref) => { - let ident_id = *ident_id_ref; - - self.by_ident.remove(&old_ident); - self.by_ident.insert(new_ident_name.into(), ident_id); + let ident_id = ident_id_ref; let by_id = &mut self.by_id; let key_index_opt = by_id.iter().position(|x| *x == old_ident); @@ -621,7 +610,7 @@ impl IdentIds { } None => Err(format!( "Tried to update key in IdentIds ({:?}) but I could not find the key ({}).", - self.by_ident, old_ident_name + self.by_id, old_ident_name )), } } @@ -648,8 +637,15 @@ impl IdentIds { self.add(ident) } - pub fn get_id(&self, ident_name: &Ident) -> Option<&IdentId> { - self.by_ident.get(ident_name) + #[inline(always)] + pub fn get_id(&self, ident_name: &Ident) -> Option { + for (id, ident) in self.idents() { + if ident_name == ident { + return Some(id); + } + } + + None } pub fn get_name(&self, id: IdentId) -> Option<&Ident> { @@ -694,20 +690,8 @@ macro_rules! define_builtins { $ident_name.into(), )+ ]; - let mut by_ident = MutMap::with_capacity_and_hasher(by_id.len(), default_hasher()); - - $( - debug_assert!(by_ident.len() == $ident_id, "Error setting up Builtins: when inserting {} …: {:?} into module {} …: {:?} - this entry was assigned an ID of {}, but based on insertion order, it should have had an ID of {} instead! To fix this, change it from {} …: {:?} to {} …: {:?} instead.", $ident_id, $ident_name, $module_id, $module_name, $ident_id, by_ident.len(), $ident_id, $ident_name, by_ident.len(), $ident_name); - - let exists = by_ident.insert($ident_name.into(), IdentId($ident_id)); - - if let Some(_) = exists { - debug_assert!(false, "Error setting up Builtins: when inserting {} …: {:?} into module {} …: {:?} - the Ident name {:?} is already present in the map. Check the map for duplicate ident names within the {:?} module!", $ident_id, $ident_name, $module_id, $module_name, $ident_name, $module_name); - } - )+ IdentIds { - by_ident, by_id, next_generated_name: 0, }