Skip to content

Commit 3693548

Browse files
authored
Merge pull request #254 from epage/perf
perf(dict): Bypass vars when possible
2 parents e6c595c + b99f32d commit 3693548

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

crates/typos-vars/codegen/src/main.rs

+16
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ fn generate_variations<W: std::io::Write>(file: &mut W) {
7878

7979
let mut smallest = usize::MAX;
8080
let mut largest = usize::MIN;
81+
let mut no_invalid = true;
8182

8283
writeln!(
8384
file,
@@ -97,6 +98,8 @@ fn generate_variations<W: std::io::Write>(file: &mut W) {
9798
builder.entry(unicase::UniCase::new(word), &value);
9899
smallest = std::cmp::min(smallest, word.len());
99100
largest = std::cmp::max(largest, word.len());
101+
102+
no_invalid &= !is_always_invalid(data);
100103
}
101104
let codegenned = builder.build();
102105
writeln!(file, "{}", codegenned).unwrap();
@@ -110,6 +113,10 @@ fn generate_variations<W: std::io::Write>(file: &mut W) {
110113
)
111114
.unwrap();
112115

116+
writeln!(file).unwrap();
117+
writeln!(file, "pub const NO_INVALID: bool = {:?};", no_invalid,).unwrap();
118+
119+
writeln!(file).unwrap();
113120
for (symbol, entry) in entries.iter() {
114121
if !referenced_symbols.contains(symbol.as_str()) {
115122
continue;
@@ -156,6 +163,15 @@ fn is_always_valid(data: &[(&str, varcon::CategorySet)]) -> bool {
156163
false
157164
}
158165

166+
fn is_always_invalid(data: &[(&str, varcon::CategorySet)]) -> bool {
167+
for (_symbol, set) in data.iter() {
168+
if set.is_empty() {
169+
return true;
170+
}
171+
}
172+
false
173+
}
174+
159175
fn entries() -> BTreeMap<String, varcon_core::Entry> {
160176
varcon::VARCON
161177
.iter()

crates/typos-vars/src/vars_codegen.rs

+3
Original file line numberDiff line numberDiff line change
@@ -113083,6 +113083,9 @@ pub static VARS_DICTIONARY: phf::Map<
113083113083
};
113084113084

113085113085
pub const WORD_RANGE: std::ops::RangeInclusive<usize> = 2..=24;
113086+
113087+
pub const NO_INVALID: bool = true;
113088+
113086113089
pub(crate) static ENTRY_ABETTORS_7043394254318611656: VariantsMap =
113087113090
[&["abettors"], &["abetters"], &["abettors"], &["abetters"]];
113088113091

src/dict.rs

+45-30
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ impl BuiltIn {
4848
.for_each(|mut s| case_correct(&mut s, word_token.case()));
4949
Some(corrections)
5050
}
51+
}
5152

52-
#[cfg(feature = "dict")]
53+
#[cfg(feature = "dict")]
54+
impl BuiltIn {
5355
// Not using `Status` to avoid the allocations
5456
fn correct_with_dict(&self, word: &str) -> Option<&'static [&'static str]> {
5557
if typos_dict::WORD_RANGE.contains(&word.len()) {
@@ -58,53 +60,55 @@ impl BuiltIn {
5860
None
5961
}
6062
}
63+
}
6164

62-
#[cfg(not(feature = "dict"))]
65+
#[cfg(not(feature = "dict"))]
66+
impl BuiltIn {
6367
fn correct_with_dict(&self, _word: &str) -> Option<&'static [&'static str]> {
6468
None
6569
}
70+
}
6671

67-
#[cfg(feature = "vars")]
72+
#[cfg(feature = "vars")]
73+
impl BuiltIn {
6874
fn chain_with_vars(&self, corrections: &'static [&'static str]) -> Status<'static> {
69-
let mut chained: Vec<_> = corrections
70-
.iter()
71-
.flat_map(|c| match self.correct_with_vars(c) {
72-
Some(Status::Valid) | None => vec![Cow::Borrowed(*c)],
73-
Some(Status::Corrections(vars)) => vars,
74-
Some(Status::Invalid) => {
75-
unreachable!("correct_with_vars should always have valid suggestions")
76-
}
77-
})
78-
.collect();
79-
if chained.len() != 1 {
80-
chained.sort_unstable();
81-
chained.dedup();
75+
if self.is_vars_enabled() {
76+
let mut chained: Vec<_> = corrections
77+
.iter()
78+
.flat_map(|c| match self.correct_with_vars(c) {
79+
Some(Status::Valid) | None => vec![Cow::Borrowed(*c)],
80+
Some(Status::Corrections(vars)) => vars,
81+
Some(Status::Invalid) => {
82+
unreachable!("correct_with_vars should always have valid suggestions")
83+
}
84+
})
85+
.collect();
86+
if chained.len() != 1 {
87+
chained.sort_unstable();
88+
chained.dedup();
89+
}
90+
debug_assert!(!chained.is_empty());
91+
Status::Corrections(chained)
92+
} else {
93+
Status::Corrections(corrections.iter().map(|c| Cow::Borrowed(*c)).collect())
8294
}
83-
debug_assert!(!chained.is_empty());
84-
Status::Corrections(chained)
85-
}
86-
87-
#[cfg(not(feature = "vars"))]
88-
fn chain_with_vars(&self, corrections: &'static [&'static str]) -> Status<'static> {
89-
Status::Corrections(corrections.iter().map(|c| Cow::Borrowed(*c)).collect())
9095
}
9196

92-
#[cfg(feature = "vars")]
9397
fn correct_with_vars(&self, word: &str) -> Option<Status<'static>> {
94-
if typos_vars::WORD_RANGE.contains(&word.len()) {
98+
if self.is_vars_enabled() && typos_vars::WORD_RANGE.contains(&word.len()) {
9599
map_lookup(&typos_vars::VARS_DICTIONARY, word)
96100
.map(|variants| self.select_variant(variants))
97101
} else {
98102
None
99103
}
100104
}
101105

102-
#[cfg(not(feature = "vars"))]
103-
fn correct_with_vars(&self, _word: &str) -> Option<Status<'static>> {
104-
None
106+
fn is_vars_enabled(&self) -> bool {
107+
#![allow(clippy::assertions_on_constants)]
108+
debug_assert!(typos_vars::NO_INVALID);
109+
self.locale.is_some()
105110
}
106111

107-
#[cfg(feature = "vars")]
108112
fn select_variant(
109113
&self,
110114
vars: &'static [(u8, &'static typos_vars::VariantsMap)],
@@ -148,6 +152,17 @@ impl BuiltIn {
148152
}
149153
}
150154

155+
#[cfg(not(feature = "vars"))]
156+
impl BuiltIn {
157+
fn chain_with_vars(&self, corrections: &'static [&'static str]) -> Status<'static> {
158+
Status::Corrections(corrections.iter().map(|c| Cow::Borrowed(*c)).collect())
159+
}
160+
161+
fn correct_with_vars(&self, _word: &str) -> Option<Status<'static>> {
162+
None
163+
}
164+
}
165+
151166
impl typos::Dictionary for BuiltIn {
152167
fn correct_ident<'s, 'w>(&'s self, ident: typos::tokens::Identifier<'w>) -> Option<Status<'s>> {
153168
BuiltIn::correct_ident(self, ident)
@@ -296,7 +311,7 @@ mod test {
296311
typos::tokens::Case::Lower,
297312
0,
298313
));
299-
assert_eq!(correction, Some(Status::Valid));
314+
assert_eq!(correction, None);
300315
}
301316

302317
#[cfg(feature = "vars")]

0 commit comments

Comments
 (0)