perf(dictgen): Make lookups faster for map/table

This commit is contained in:
Ed Page 2024-12-30 11:01:58 -06:00
parent db16cb53c5
commit 083871447d
2 changed files with 4 additions and 0 deletions

View file

@ -58,6 +58,7 @@ pub struct DictMap<V: 'static> {
} }
impl<V> DictMap<V> { impl<V> DictMap<V> {
#[inline]
pub fn find(&self, word: &'_ unicase::UniCase<&str>) -> Option<&V> { pub fn find(&self, word: &'_ unicase::UniCase<&str>) -> Option<&V> {
if self.range.contains(&word.len()) { if self.range.contains(&word.len()) {
self.map.get(&(*word).into()) self.map.get(&(*word).into())
@ -66,6 +67,7 @@ impl<V> DictMap<V> {
} }
} }
#[inline]
pub fn iter(&self) -> impl Iterator<Item = (unicase::UniCase<&str>, &V)> + '_ { pub fn iter(&self) -> impl Iterator<Item = (unicase::UniCase<&str>, &V)> + '_ {
self.map.entries().map(|(k, v)| (k.convert(), v)) self.map.entries().map(|(k, v)| (k.convert(), v))
} }

View file

@ -59,6 +59,7 @@ pub struct DictTable<V: 'static> {
} }
impl<V> DictTable<V> { impl<V> DictTable<V> {
#[inline]
pub fn find(&self, word: &'_ unicase::UniCase<&str>) -> Option<&'static V> { pub fn find(&self, word: &'_ unicase::UniCase<&str>) -> Option<&'static V> {
if self.range.contains(&word.len()) { if self.range.contains(&word.len()) {
self.keys self.keys
@ -70,6 +71,7 @@ impl<V> DictTable<V> {
} }
} }
#[inline]
pub fn iter(&self) -> impl Iterator<Item = (unicase::UniCase<&'static str>, &'static V)> + '_ { pub fn iter(&self) -> impl Iterator<Item = (unicase::UniCase<&'static str>, &'static V)> + '_ {
(0..self.keys.len()).map(move |i| (self.keys[i].convert(), &self.values[i])) (0..self.keys.len()).map(move |i| (self.keys[i].convert(), &self.values[i]))
} }