mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 15:21:12 +00:00
flip the order of loops to make lookups much faster
This commit is contained in:
parent
a78aff0f2f
commit
c1d9c63e37
3 changed files with 28 additions and 23 deletions
|
@ -431,14 +431,10 @@ impl ScopedIdentIds {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_in_scope(&self, ident: &Ident) -> Option<(Symbol, Region)> {
|
fn has_in_scope(&self, ident: &Ident) -> Option<(Symbol, Region)> {
|
||||||
for index in self.in_scope.iter_ones() {
|
for ident_id in self.ident_ids.get_id_many(ident) {
|
||||||
if let Some((ident_id, string)) = self.ident_ids.get_name_at_index(index) {
|
let index = ident_id.index();
|
||||||
if string == ident.as_str() {
|
if self.in_scope[index] {
|
||||||
return Some((
|
return Some((Symbol::new(self.home, ident_id), self.regions[index]));
|
||||||
Symbol::new(self.home, ident_id),
|
|
||||||
self.regions[ident_id.index()],
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -86,22 +86,30 @@ impl SmallStringInterner {
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn find_index(&self, string: &str) -> Option<usize> {
|
pub fn find_index(&self, string: &str) -> Option<usize> {
|
||||||
|
self.find_indices(string).next()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn find_indices<'a>(&'a self, string: &'a str) -> impl Iterator<Item = usize> + 'a {
|
||||||
let target_length = string.len() as u16;
|
let target_length = string.len() as u16;
|
||||||
|
|
||||||
// there can be gaps in the parts of the string that we use (because of updates)
|
// 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
|
// 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() {
|
self.lengths
|
||||||
if *length == target_length {
|
.iter()
|
||||||
let offset = self.offsets[index];
|
.enumerate()
|
||||||
let slice = &self.buffer[offset as usize..][..*length as usize];
|
.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 {
|
if string.as_bytes() == slice {
|
||||||
return Some(index);
|
return Some(index);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
None
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(&self, index: usize) -> &str {
|
fn get(&self, index: usize) -> &str {
|
||||||
|
|
|
@ -580,14 +580,15 @@ impl IdentIds {
|
||||||
.map(|i| IdentId(i as u32))
|
.map(|i| IdentId(i as u32))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_name(&self, id: IdentId) -> Option<&str> {
|
#[inline(always)]
|
||||||
self.interner.try_get(id.0 as usize)
|
pub fn get_id_many<'a>(&'a self, ident_name: &'a Ident) -> impl Iterator<Item = IdentId> + '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)> {
|
pub fn get_name(&self, id: IdentId) -> Option<&str> {
|
||||||
self.interner
|
self.interner.try_get(id.0 as usize)
|
||||||
.try_get(index)
|
|
||||||
.map(|v| (IdentId(index as u32), v))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_name_str_res(&self, ident_id: IdentId) -> ModuleResult<&str> {
|
pub fn get_name_str_res(&self, ident_id: IdentId) -> ModuleResult<&str> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue