Skip to content

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

Started using `SmallVec`s in Dictionary

Elijah Potter4468e08d7ce8
diff --git a/Cargo.lock b/Cargo.lock
index 5ae56d6d4..fbd66b9de 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -30,7 +30,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c3a9648d43b9cd48db467b3f87fdd6e146bcc88ab0180006cef2179fe11d01"
dependencies = [
"cfg-if",
- "getrandom",
"once_cell",
"version_check",
"zerocopy",
@@ -614,8 +613,8 @@ checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
name = "harper-core"
version = "0.1.0"
dependencies = [
- "ahash",
"criterion",
+ "hashbrown",
"is-macro",
"itertools 0.11.0",
"once_cell",
diff --git a/harper-core/Cargo.toml b/harper-core/Cargo.toml
index a28d6b58f..13025fcd8 100644
--- a/harper-core/Cargo.toml
+++ b/harper-core/Cargo.toml
@@ -6,7 +6,7 @@ description = "The language checker for artists."
license = "MIT OR Apache-2.0"
[dependencies]
-ahash = "0.8.7"
+hashbrown = "0.14.3"
is-macro = "0.3.0"
itertools = "0.11.0"
once_cell = "1.19.0"
diff --git a/harper-core/benches/spellcheck.rs b/harper-core/benches/spellcheck.rs
index ff1886c3a..eb4fd7194 100644
--- a/harper-core/benches/spellcheck.rs
+++ b/harper-core/benches/spellcheck.rs
@@ -1,17 +1,36 @@
use criterion::{criterion_group, criterion_main, Criterion};
use harper_core::{suggest_correct_spelling_str, Dictionary};
-fn spellcheck(dictionary: &Dictionary) {
- suggest_correct_spelling_str("hello", 5, 3, dictionary);
-}
-
fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("spellcheck");
let dictionary = Dictionary::new();
group.bench_function("dict create", |b| b.iter(Dictionary::new));
- group.bench_function("hellw 5", |b| b.iter(|| spellcheck(dictionary)));
+
+ group.bench_function("hellw 5", |b| {
+ b.iter(|| {
+ suggest_correct_spelling_str("hellw", 5, 3, dictionary);
+ })
+ });
+
+ group.bench_function("thw 5", |b| {
+ b.iter(|| {
+ suggest_correct_spelling_str("thw", 5, 3, dictionary);
+ })
+ });
+
+ group.bench_function("thw 10", |b| {
+ b.iter(|| {
+ suggest_correct_spelling_str("thw", 10, 3, dictionary);
+ })
+ });
+
+ group.bench_function("hecatoncheires 10", |b| {
+ b.iter(|| {
+ suggest_correct_spelling_str("hecatoncheires", 10, 3, dictionary);
+ })
+ });
}
criterion_group!(benches, criterion_benchmark);
diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rs
index 5772c8c8c..aebc8a840 100644
--- a/harper-core/src/linting/repeated_words.rs
+++ b/harper-core/src/linting/repeated_words.rs
@@ -1,4 +1,4 @@
-use ahash::HashSet;
+use hashbrown::HashSet;
use crate::{
parsing::TokenStringExt, Dictionary, Document, Lint, LintKind, Span, Suggestion, Token,
@@ -51,7 +51,7 @@ pub fn repeated_words(document: &Document, _dictionary: &Dictionary) -> Vec<Lint
/// The set of words that can be considered for repetition checking.
fn create_match_set() -> HashSet<Vec<char>> {
- let mut output = HashSet::default();
+ let mut output = HashSet::new();
output.insert(vec!['t', 'h', 'e']);
output.insert(vec!['T', 'h', 'e']);
diff --git a/harper-core/src/spell/dictionary.rs b/harper-core/src/spell/dictionary.rs
index 661c71864..018ec83ec 100644
--- a/harper-core/src/spell/dictionary.rs
+++ b/harper-core/src/spell/dictionary.rs
@@ -1,11 +1,11 @@
-use std::hash::Hasher;
-
-use ahash::{AHashSet, AHasher};
+use hashbrown::HashSet;
use once_cell::sync::Lazy;
+use smallvec::SmallVec;
-use super::hunspell::{parse_default_attribute_list, parse_default_word_list};
-
-type DictWord = Vec<char>;
+use super::{
+ hunspell::{parse_default_attribute_list, parse_default_word_list},
+ DictWord,
+};
#[derive(Debug, Clone)]
pub struct Dictionary {
@@ -20,40 +20,33 @@ pub struct Dictionary {
/// Each index of this list will return the first index of [`Self::words`] that has a word
/// whose index is that length.
word_len_starts: Vec<usize>,
- word_set: AHashSet<u64>,
+ word_set: HashSet<DictWord>,
}
fn uncached_inner_new() -> Dictionary {
let word_list = parse_default_word_list().unwrap();
let attr_list = parse_default_attribute_list().unwrap();
- let mut words: Vec<Vec<char>> = attr_list.expand_marked_words(word_list).unwrap();
+ let words = attr_list.expand_marked_words(word_list).unwrap();
+ let mut words: Vec<DictWord> = words.into_iter().collect();
words.sort_by_key(|a| a.len());
let mut word_len_starts = vec![0, 0];
- for (index, len) in words.iter().map(Vec::len).enumerate() {
+ for (index, len) in words.iter().map(SmallVec::len).enumerate() {
if word_len_starts.len() == len {
word_len_starts.push(index);
}
}
Dictionary {
- word_set: AHashSet::from_iter(words.iter().map(|v| hash_word(v.as_slice()))),
+ word_set: HashSet::from_iter(words.iter().cloned()),
word_len_starts,
words,
}
}
-fn hash_word(word: &[char]) -> u64 {
- let mut hasher = AHasher::default();
- for c in word {
- hasher.write_u32(*c as u32);
- }
- hasher.finish()
-}
-
static DICT: Lazy<Dictionary> = Lazy::new(uncached_inner_new);
impl Dictionary {
@@ -82,8 +75,8 @@ impl Dictionary {
}
pub fn contains_word(&self, word: &[char]) -> bool {
- let lowercase: Vec<_> = word.iter().flat_map(|c| c.to_lowercase()).collect();
+ let lowercase: SmallVec<_> = word.iter().flat_map(|c| c.to_lowercase()).collect();
- self.word_set.contains(&hash_word(word)) || self.word_set.contains(&hash_word(&lowercase))
+ self.word_set.contains(word) || self.word_set.contains(&lowercase)
}
}
diff --git a/harper-core/src/spell/hunspell/attributes.rs b/harper-core/src/spell/hunspell/attributes.rs
index c4c7ca271..95e5bbd27 100644
--- a/harper-core/src/spell/hunspell/attributes.rs
+++ b/harper-core/src/spell/hunspell/attributes.rs
@@ -1,9 +1,10 @@
use itertools::Itertools;
+use smallvec::ToSmallVec;
use std::usize;
-use ahash::HashMap;
+use hashbrown::HashMap;
-use crate::Span;
+use crate::{spell::DictWord, Span};
use super::{matcher::Matcher, word_list::MarkedWord, Error};
@@ -107,7 +108,7 @@ impl AttributeList {
/// Expand [`MarkedWord`] into a list of full words, including itself.
///
/// In the future, I want to make this function cleaner and faster.
- pub fn expand_marked_word(&self, word: MarkedWord) -> Result<Vec<Vec<char>>, Error> {
+ pub fn expand_marked_word(&self, word: MarkedWord) -> Result<Vec<DictWord>, Error> {
let mut words = Vec::with_capacity(word.attributes.len() + 1);
for attr in &word.attributes {
@@ -160,7 +161,7 @@ impl AttributeList {
pub fn expand_marked_words(
&self,
words: impl IntoIterator<Item = MarkedWord>,
- ) -> Result<Vec<Vec<char>>, Error> {
+ ) -> Result<Vec<DictWord>, Error> {
let mut output = Vec::new();
for word in words {
@@ -174,7 +175,7 @@ impl AttributeList {
replacement: &AffixReplacement,
letters: &[char],
suffix: bool,
- ) -> Option<Vec<char>> {
+ ) -> Option<DictWord> {
if replacement.condition.len() > letters.len() {
return None;
}
@@ -188,8 +189,8 @@ impl AttributeList {
let target_segment = target_span.get_content(letters);
if replacement.condition.matches(target_segment) {
- let mut replaced_segment = letters.to_vec();
- let mut remove = replacement.remove.to_vec();
+ let mut replaced_segment = letters.to_smallvec();
+ let mut remove: DictWord = replacement.remove.to_smallvec();
if !suffix {
replaced_segment.reverse();
diff --git a/harper-core/src/spell/hunspell/mod.rs b/harper-core/src/spell/hunspell/mod.rs
index 94e2a9709..a8fb59d92 100644
--- a/harper-core/src/spell/hunspell/mod.rs
+++ b/harper-core/src/spell/hunspell/mod.rs
@@ -18,6 +18,8 @@ pub fn parse_default_attribute_list() -> Result<AttributeList, Error> {
#[cfg(test)]
mod tests {
+ use crate::spell::DictWord;
+
use super::{
attributes::AttributeList, parse_default_attribute_list, parse_default_word_list,
word_list::parse_word_list,
@@ -55,7 +57,7 @@ mod tests {
assert!(expanded.contains(&split("giants")))
}
- fn build_expanded() -> Vec<Vec<char>> {
+ fn build_expanded() -> Vec<DictWord> {
let words = parse_default_word_list().unwrap();
let attributes = parse_default_attribute_list().unwrap();
@@ -72,7 +74,7 @@ mod tests {
assert!(build_expanded().contains(&split("giants")));
}
- fn split(text: &str) -> Vec<char> {
+ fn split(text: &str) -> DictWord {
text.chars().collect()
}
}
diff --git a/harper-core/src/spell/hunspell/word_list.rs b/harper-core/src/spell/hunspell/word_list.rs
index c58ec4c8f..c3bf404e1 100644
--- a/harper-core/src/spell/hunspell/word_list.rs
+++ b/harper-core/src/spell/hunspell/word_list.rs
@@ -1,7 +1,9 @@
+use crate::spell::DictWord;
+
use super::Error;
pub struct MarkedWord {
- pub letters: Vec<char>,
+ pub letters: DictWord,
pub attributes: Vec<char>,
}
diff --git a/harper-core/src/spell/mod.rs b/harper-core/src/spell/mod.rs
index cf18aa1f7..88e2156db 100644
--- a/harper-core/src/spell/mod.rs
+++ b/harper-core/src/spell/mod.rs
@@ -1,8 +1,12 @@
+use smallvec::SmallVec;
+
pub use self::dictionary::Dictionary;
mod dictionary;
mod hunspell;
+type DictWord = SmallVec<[char; 6]>;
+
/// Suggest a correct spelling for a given misspelled word.
/// [misspelled_word] is assumed to be quite small (n < 100)
/// [max_edit_dist] relates to an optimization that allows the search algorithm to prune large portions of the search.