Skip to content

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

feat(fst-dict): remove memmap and resolve errors

Grant Lemons1e80e9c5d305
diff --git a/Cargo.lock b/Cargo.lock
index 79ff55e99..da7e3c7c9 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -603,7 +603,6 @@ dependencies = [
"hashbrown 0.15.0",
"is-macro",
"itertools 0.13.0",
- "memmap",
"ordered-float",
"paste",
"pulldown-cmark",
@@ -996,16 +995,6 @@ version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
-[[package]]
-name = "memmap"
-version = "0.7.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6585fd95e7bb50d6cc31e20d4cf9afb4e2ba16c5846fc76793f11218da9c475b"
-dependencies = [
- "libc",
- "winapi",
-]
-
[[package]]
name = "miniz_oxide"
version = "0.8.0"
diff --git a/harper-core/Cargo.toml b/harper-core/Cargo.toml
index 81ad19589..ceeb30ffa 100644
--- a/harper-core/Cargo.toml
+++ b/harper-core/Cargo.toml
@@ -13,7 +13,6 @@ fst = { version = "0.4.7", features = ["levenshtein"] }
hashbrown = { version = "0.15.0", features = ["serde"] }
is-macro = "0.3.6"
itertools = "0.13.0"
-memmap = "0.7.0"
ordered-float = { version = "4.5.0", features = ["serde"] }
paste = "1.0.14"
pulldown-cmark = "0.12.2"
diff --git a/harper-core/src/spell/fst_dictionary.rs b/harper-core/src/spell/fst_dictionary.rs
index 046f95e85..3cee520a5 100644
--- a/harper-core/src/spell/fst_dictionary.rs
+++ b/harper-core/src/spell/fst_dictionary.rs
@@ -1,11 +1,10 @@
-use std::fs::File;
+use core::str;
use fst::Map as FstMap;
use fst::{automaton::Levenshtein, IntoStreamer};
-use memmap::Mmap;
use super::hunspell::{parse_default_attribute_list, parse_default_word_list};
-use crate::{Lrc, WordMetadata};
+use crate::{CharString, Lrc, WordMetadata};
use super::Dictionary;
@@ -13,8 +12,10 @@ use super::Dictionary;
pub struct FstDictionary {
/// Sorted by string in lexicographic order
metadata: Vec<WordMetadata>,
+ /// Sorted in lexicographic order
+ words: Vec<CharString>,
/// Used for fuzzy-finding the index of words or metadata
- word_map: FstMap<Mmap>,
+ word_map: FstMap<Vec<u8>>,
}
/// The uncached function that is used to produce the original copy of the
@@ -26,10 +27,9 @@ fn uncached_inner_new() -> Lrc<FstDictionary> {
// There will be at _least_ this number of words
// This creates a memory map, which enables searching the map without loading
// all of it into memory.
- let mmap = unsafe { Mmap::map(&File::open("../../dictionary.fst").unwrap()).unwrap() };
- let word_map = FstMap::new(mmap).unwrap();
+ let word_map = FstMap::new(include_bytes!("../../dictionary.fst").to_vec()).unwrap();
- let mut words: Vec<&[char]> = word_list
+ let mut words: Vec<CharString> = word_list
.iter()
.map(|mw| mw.letters.as_ref().into())
.collect();
@@ -37,7 +37,11 @@ fn uncached_inner_new() -> Lrc<FstDictionary> {
words.dedup();
let metadata: Vec<WordMetadata> = todo!();
- Lrc::new(FstDictionary { metadata, word_map })
+ Lrc::new(FstDictionary {
+ metadata,
+ words,
+ word_map,
+ })
}
thread_local! {
@@ -79,11 +83,12 @@ impl Dictionary for FstDictionary {
max_results: usize,
) -> Vec<(&[char], u8, WordMetadata)> {
let aut = Levenshtein::new(word, max_distance as u32).unwrap();
- let words: Vec<(Vec<u8>, u64)> = self.word_map.search(aut).into_stream().into_byte_vec();
- words
+ let word_indexes: Vec<u64> = self.word_map.search(aut).into_stream().into_values();
+ word_indexes
.into_iter()
.take(max_results)
- .map(|(word, i)| (word, i as u8, self.metadata[i as usize]))
+ .map(|i| (self.words.get(i as usize).unwrap(), i))
+ .map(|(word, i)| (word.as_slice(), i as u8, self.metadata[i as usize]))
.collect()
}
}