Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(core): avoid more allocation by using `Cow`

Elijah Potter9a018f49d112
diff --git a/harper-core/src/char_string.rs b/harper-core/src/char_string.rs
index f6df85487..ec65ac7fc 100644
--- a/harper-core/src/char_string.rs
+++ b/harper-core/src/char_string.rs
@@ -1,3 +1,5 @@
+use std::borrow::Cow;
+
use smallvec::SmallVec;
/// A char sequence that improves cache locality.
@@ -6,18 +8,23 @@ pub type CharString = SmallVec<[char; 16]>;
/// Extensions to character sequences that make them easier to wrangle.
pub trait CharStringExt {
- fn to_lower(&self) -> CharString;
+ fn to_lower(&self) -> Cow<[char]>;
fn to_string(&self) -> String;
}
impl CharStringExt for [char] {
- fn to_lower(&self) -> CharString {
+ fn to_lower(&self) -> Cow<[char]> {
+ if self.iter().all(|c| c.is_lowercase()) {
+ return Cow::Borrowed(self);
+ }
+
let mut out = CharString::with_capacity(self.len());
out.extend(self.iter().flat_map(|v| v.to_lowercase()));
- out
+ Cow::Owned(out.to_vec())
}
+
fn to_string(&self) -> String {
self.iter().collect()
}
diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rs
index 8a73ebef8..79fb4ac39 100644
--- a/harper-core/src/linting/repeated_words.rs
+++ b/harper-core/src/linting/repeated_words.rs
@@ -27,7 +27,7 @@ impl RepeatedWords {
self.special_cases
.iter()
- .any(|v| v.as_slice() == lower.as_slice())
+ .any(|v| v.as_slice() == lower.as_ref())
}
}
diff --git a/harper-core/src/spell/mutable_dictionary.rs b/harper-core/src/spell/mutable_dictionary.rs
index 3822633d8..8b879165b 100644
--- a/harper-core/src/spell/mutable_dictionary.rs
+++ b/harper-core/src/spell/mutable_dictionary.rs
@@ -64,7 +64,7 @@ fn uncached_inner_new() -> Arc<MutableDictionary> {
let mut word_map_lowercase = HashMap::with_capacity(word_map.len());
for key in word_map.keys() {
- word_map_lowercase.insert(key.to_lower(), key.clone());
+ word_map_lowercase.insert(key.to_lower().to_smallvec(), key.clone());
}
Arc::new(MutableDictionary {
@@ -113,8 +113,11 @@ impl MutableDictionary {
self.words.extend(pairs.iter().map(|(v, _)| v.clone()));
self.words.sort_by_key(|w| w.len());
self.word_len_starts = Self::create_len_starts(&self.words);
- self.word_map_lowercase
- .extend(pairs.iter().map(|(key, _)| (key.to_lower(), key.clone())));
+ self.word_map_lowercase.extend(
+ pairs
+ .iter()
+ .map(|(key, _)| (key.to_lower().to_smallvec(), key.clone())),
+ );
self.word_map.extend(pairs);
}
@@ -169,9 +172,9 @@ impl Dictionary for MutableDictionary {
fn contains_word(&self, word: &[char]) -> bool {
let normalized = seq_to_normalized(word);
- let lowercase: CharString = normalized.to_lower();
+ let lowercase = normalized.to_lower();
- self.word_map_lowercase.contains_key(&lowercase)
+ self.word_map_lowercase.contains_key(lowercase.as_ref())
}
fn contains_word_str(&self, word: &str) -> bool {
@@ -186,10 +189,10 @@ impl Dictionary for MutableDictionary {
fn get_correct_capitalization_of(&self, word: &[char]) -> Option<&'_ [char]> {
let normalized = seq_to_normalized(word);
- let lowercase: CharString = normalized.to_lower();
+ let lowercase = normalized.to_lower();
self.word_map_lowercase
- .get(&lowercase)
+ .get(lowercase.as_ref())
.map(|v| v.as_slice())
}
diff --git a/harper-core/src/title_case.rs b/harper-core/src/title_case.rs
index 8c4b2b7da..b504151ac 100644
--- a/harper-core/src/title_case.rs
+++ b/harper-core/src/title_case.rs
@@ -97,7 +97,7 @@ fn should_capitalize_token(tok: &Token, source: &[char], dict: &impl Dictionary)
!is_short_preposition
&& !metadata.article
- && !SPECIAL_CONJUNCTIONS.contains(chars_lower.as_slice())
+ && !SPECIAL_CONJUNCTIONS.contains(chars_lower.as_ref())
}
_ => true,
}