Skip to content

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

fix(ls): cache being busted on every modification

Elijah Potter7b71c1151d61
diff --git a/Cargo.lock b/Cargo.lock
index dbab6f679..eaece4973 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -736,8 +736,10 @@ dependencies = [
"blanket",
"cached",
"criterion",
+ "foldhash",
"fst",
"hashbrown 0.15.2",
+ "indexmap",
"is-macro",
"itertools 0.14.0",
"lazy_static",
@@ -1032,6 +1034,7 @@ checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652"
dependencies = [
"equivalent",
"hashbrown 0.15.2",
+ "serde",
]
[[package]]
diff --git a/harper-core/Cargo.toml b/harper-core/Cargo.toml
index ba032de49..230905c85 100644
--- a/harper-core/Cargo.toml
+++ b/harper-core/Cargo.toml
@@ -27,6 +27,8 @@ unicode-width = "0.2.0"
levenshtein_automata = { version = "0.2.1", features = ["fst_automaton"] }
cached = "0.54.0"
lru = "0.13.0"
+foldhash = "0.1.4"
+indexmap = { version = "2.7.1", features = ["serde"] }
[dev-dependencies]
criterion = { version = "0.5.1", default-features = false }
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 07ad82c73..962d70f5e 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -4,6 +4,7 @@ use std::sync::Arc;
use cached::proc_macro::cached;
use hashbrown::HashMap;
+use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use super::Lint;
@@ -67,7 +68,7 @@ use std::num::NonZero;
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
#[serde(transparent)]
pub struct LintGroupConfig {
- inner: HashMap<String, Option<bool>>,
+ inner: IndexMap<String, Option<bool>>,
}
#[cached]
@@ -85,7 +86,7 @@ impl LintGroupConfig {
/// Remove any configuration attached to a rule.
/// This allows it to assume its default (curated) state.
pub fn unset_rule_enabled(&mut self, key: impl AsRef<str>) {
- self.inner.remove_entry(key.as_ref());
+ self.inner.swap_remove_entry(key.as_ref());
}
pub fn set_rule_enabled_if_unset(&mut self, key: impl AsRef<str>, val: bool) {
@@ -111,7 +112,7 @@ impl LintGroupConfig {
///
/// Conflicting keys will be overridden by the value in the other group.
pub fn merge_from(&mut self, other: &mut LintGroupConfig) {
- for (key, val) in other.inner.drain() {
+ for (key, val) in other.inner.drain(..) {
if val.is_none() {
continue;
}
diff --git a/harper-core/src/spell/merged_dictionary.rs b/harper-core/src/spell/merged_dictionary.rs
index 9d7eec8a2..315eaf733 100644
--- a/harper-core/src/spell/merged_dictionary.rs
+++ b/harper-core/src/spell/merged_dictionary.rs
@@ -1,5 +1,7 @@
+use std::hash::{BuildHasher, Hasher};
use std::sync::Arc;
+use foldhash::fast::FixedState;
use itertools::Itertools;
use super::{FuzzyMatchResult, dictionary::Dictionary};
@@ -10,23 +12,32 @@ use crate::{CharString, WordMetadata};
#[derive(Clone)]
pub struct MergedDictionary {
children: Vec<Arc<dyn Dictionary>>,
+ child_hashes: Vec<u64>,
}
impl MergedDictionary {
pub fn new() -> Self {
Self {
children: Vec::new(),
+ child_hashes: Vec::new(),
}
}
pub fn add_dictionary(&mut self, dictionary: Arc<dyn Dictionary>) {
+ let mut hasher = FixedState::default().build_hasher();
+
+ dictionary
+ .words_iter()
+ .for_each(|w| w.iter().for_each(|c| hasher.write_u32(*c as u32)));
+
+ self.child_hashes.push(hasher.finish());
self.children.push(dictionary);
}
}
impl PartialEq for MergedDictionary {
- fn eq(&self, _other: &Self) -> bool {
- false
+ fn eq(&self, other: &Self) -> bool {
+ self.child_hashes == other.child_hashes
}
}
diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rs
index e4fbf932e..42c32af4d 100644
--- a/harper-ls/src/backend.rs
+++ b/harper-ls/src/backend.rs
@@ -157,16 +157,21 @@ impl Backend {
let mut doc_lock = self.doc_state.lock().await;
- let doc_state = doc_lock.entry(url.clone()).or_insert(DocumentState {
- linter: LintGroup::new_curated(dict.clone()).with_lint_config(lint_config.clone()),
- language_id: language_id.map(|v| v.to_string()),
- dict: dict.clone(),
- url: url.clone(),
- ..Default::default()
+ let doc_state = doc_lock.entry(url.clone()).or_insert_with(|| {
+ info!("Constructing new LintGroup for new document.");
+
+ DocumentState {
+ linter: LintGroup::new_curated(dict.clone()).with_lint_config(lint_config.clone()),
+ language_id: language_id.map(|v| v.to_string()),
+ dict: dict.clone(),
+ url: url.clone(),
+ ..Default::default()
+ }
});
if doc_state.dict != dict {
doc_state.dict = dict.clone();
+ info!("Constructing new linter because of modified dictionary.");
doc_state.linter =
LintGroup::new_curated(dict.clone()).with_lint_config(lint_config.clone());
}
@@ -185,6 +190,7 @@ impl Backend {
lint_config: &LintGroupConfig,
) -> Result<Box<dyn Parser>> {
if doc_state.ident_dict != new_dict {
+ info!("Constructing new linter because of modified ident dictionary.");
doc_state.ident_dict = new_dict.clone();
let mut merged = backend.generate_file_dictionary(url).await?;
@@ -590,6 +596,7 @@ impl LanguageServer for Backend {
let config_lock = self.config.read().await;
for doc in doc_lock.values_mut() {
+ info!("Constructing new LintGroup for updated configuration.");
doc.linter = LintGroup::new_curated(doc.dict.clone())
.with_lint_config(config_lock.lint_config.clone());
}
diff --git a/harper-ls/src/main.rs b/harper-ls/src/main.rs
index c99551564..b610b513d 100644
--- a/harper-ls/src/main.rs
+++ b/harper-ls/src/main.rs
@@ -39,7 +39,7 @@ async fn main() -> anyhow::Result<()> {
let subscriber = FmtSubscriber::builder()
.map_writer(move |_| stderr)
.with_ansi(false)
- .with_max_level(Level::WARN)
+ .with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber)?;