Skip to content

Commit

Permalink
refactor(varcon): Remove reliance on const-fn
Browse files Browse the repository at this point in the history
This dropped RSS (memory usage) from 4GB to 1.5GB when compiling.

The extra `match` could impact performance but not too concerned since
the default is to not look within vars.
  • Loading branch information
epage committed Jun 4, 2021
1 parent b1cf03c commit 2b1f565
Show file tree
Hide file tree
Showing 3 changed files with 26,899 additions and 26,887 deletions.
6 changes: 3 additions & 3 deletions crates/typos-vars/codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn generate_variations<W: std::io::Write>(file: &mut W) {

writeln!(
file,
"pub(crate) static VARS_DICTIONARY: &[(unicase::UniCase<&'static str>, &'static [(u8, &VariantsMap)])] = &["
"pub(crate) static VARS_DICTIONARY: &[(crate::EncodedStr, &[(u8, &VariantsMap)])] = &["
)
.unwrap();
let entry_sets = entry_sets(entries.iter());
Expand All @@ -93,9 +93,9 @@ fn generate_variations<W: std::io::Write>(file: &mut W) {
let value = generate_link(&data);
let word = unicase::UniCase::new(word);
let key = if word.is_ascii() {
format!("unicase::UniCase::ascii({:?})", word)
format!("crate::EncodedStr::Ascii({:?})", word)
} else {
format!("unicase::UniCase::unicode({:?})", word)
format!("crate::EncodedStr::Unicode({:?})", word)
};
writeln!(file, " ({}, {}),", key, &value).unwrap();
smallest = std::cmp::min(smallest, word.len());
Expand Down
17 changes: 16 additions & 1 deletion crates/typos-vars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,22 @@ pub use varcon_core::CategorySet;

pub fn find(word: &'_ unicase::UniCase<&str>) -> Option<&'static [(u8, &'static VariantsMap)]> {
VARS_DICTIONARY
.binary_search_by_key(word, |(key, _)| *key)
.binary_search_by_key(word, |(key, _)| key.convert())
.map(|i| VARS_DICTIONARY[i].1)
.ok()
}

#[derive(Copy, Clone, Debug)]
pub(crate) enum EncodedStr {
//Unicode(&'static str),
Ascii(&'static str),
}

impl EncodedStr {
fn convert(self) -> unicase::UniCase<&'static str> {
match self {
//EncodedStr::Unicode(s) => unicase::UniCase::unicode(s),
EncodedStr::Ascii(s) => unicase::UniCase::ascii(s),
}
}
}

0 comments on commit 2b1f565

Please sign in to comment.