diff --git a/compiler/can/src/scope.rs b/compiler/can/src/scope.rs index 25e10274d3..7546f25279 100644 --- a/compiler/can/src/scope.rs +++ b/compiler/can/src/scope.rs @@ -431,14 +431,10 @@ impl ScopedIdentIds { } fn has_in_scope(&self, ident: &Ident) -> Option<(Symbol, Region)> { - for index in self.in_scope.iter_ones() { - if let Some((ident_id, string)) = self.ident_ids.get_name_at_index(index) { - if string == ident.as_str() { - return Some(( - Symbol::new(self.home, ident_id), - self.regions[ident_id.index()], - )); - } + for ident_id in self.ident_ids.get_id_many(ident) { + let index = ident_id.index(); + if self.in_scope[index] { + return Some((Symbol::new(self.home, ident_id), self.regions[index])); } } diff --git a/compiler/collections/src/small_string_interner.rs b/compiler/collections/src/small_string_interner.rs index 387e6fcd2c..9859767b44 100644 --- a/compiler/collections/src/small_string_interner.rs +++ b/compiler/collections/src/small_string_interner.rs @@ -86,22 +86,30 @@ impl SmallStringInterner { #[inline(always)] pub fn find_index(&self, string: &str) -> Option { + self.find_indices(string).next() + } + + #[inline(always)] + pub fn find_indices<'a>(&'a self, string: &'a str) -> impl Iterator + 'a { let target_length = string.len() as u16; // there can be gaps in the parts of the string that we use (because of updates) // hence we can't just sum the lengths we've seen so far to get the next offset - for (index, length) in self.lengths.iter().enumerate() { - if *length == target_length { - let offset = self.offsets[index]; - let slice = &self.buffer[offset as usize..][..*length as usize]; + self.lengths + .iter() + .enumerate() + .filter_map(move |(index, length)| { + if *length == target_length { + let offset = self.offsets[index]; + let slice = &self.buffer[offset as usize..][..*length as usize]; - if string.as_bytes() == slice { - return Some(index); + if string.as_bytes() == slice { + return Some(index); + } } - } - } - None + None + }) } fn get(&self, index: usize) -> &str { diff --git a/compiler/module/src/symbol.rs b/compiler/module/src/symbol.rs index 3e0fe96d77..ab7385ed28 100644 --- a/compiler/module/src/symbol.rs +++ b/compiler/module/src/symbol.rs @@ -580,14 +580,15 @@ impl IdentIds { .map(|i| IdentId(i as u32)) } - pub fn get_name(&self, id: IdentId) -> Option<&str> { - self.interner.try_get(id.0 as usize) + #[inline(always)] + pub fn get_id_many<'a>(&'a self, ident_name: &'a Ident) -> impl Iterator + 'a { + self.interner + .find_indices(ident_name.as_str()) + .map(|i| IdentId(i as u32)) } - pub fn get_name_at_index(&self, index: usize) -> Option<(IdentId, &str)> { - self.interner - .try_get(index) - .map(|v| (IdentId(index as u32), v)) + pub fn get_name(&self, id: IdentId) -> Option<&str> { + self.interner.try_get(id.0 as usize) } pub fn get_name_str_res(&self, ident_id: IdentId) -> ModuleResult<&str> {