Skip to content

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

fix(ls): expand cache size

Elijah Pottere54c6dc2f713
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 962d70f5e..e06bb0a64 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -63,7 +63,6 @@ use super::{CurrencyPlacement, Linter, NoOxfordComma, OxfordComma};
use crate::Document;
use crate::linting::{closed_compounds, phrase_corrections};
use crate::{Dictionary, MutableDictionary};
-use std::num::NonZero;
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
#[serde(transparent)]
@@ -208,10 +207,7 @@ impl LintGroup {
($rule:ident, $default_config:expr) => {
out.add(
stringify!($rule),
- Box::new(PatternLinterCache::new(
- $rule::default(),
- NonZero::new(1000).unwrap(),
- )),
+ Box::new(PatternLinterCache::new_default_size($rule::default())),
);
out.config
.set_rule_enabled(stringify!($rule), $default_config);
diff --git a/harper-core/src/linting/pattern_linter.rs b/harper-core/src/linting/pattern_linter.rs
index abf9e5f5d..34af4dc6b 100644
--- a/harper-core/src/linting/pattern_linter.rs
+++ b/harper-core/src/linting/pattern_linter.rs
@@ -1,4 +1,4 @@
-use std::num::NonZeroUsize;
+use std::num::{NonZero, NonZeroUsize};
use lru::LruCache;
@@ -54,13 +54,20 @@ pub struct PatternLinterCache<P: PatternLinter> {
impl<P: PatternLinter> PatternLinterCache<P> {
/// Add a cache to a given [`PatternLinter`] with a given cache size.
- /// About a 1000 rows is recommended.
pub fn new(inner: P, cache_size: NonZeroUsize) -> Self {
Self {
cache: ChunkCache::new(cache_size),
inner,
}
}
+
+ /// Add a cache to a given [`PatternLinter`] with a given cache size.
+ pub fn new_default_size(inner: P) -> Self {
+ Self {
+ cache: ChunkCache::new(NonZero::new(100000).unwrap()),
+ inner,
+ }
+ }
}
impl<P: PatternLinter> Linter for PatternLinterCache<P> {
@@ -115,14 +122,14 @@ fn run_on_chunk_cached(
let Some(chunk_span) = chunk.span() else {
return Vec::new().into();
};
+ let chars = chunk_span.get_content(source);
- let key = chunk_span.get_content(source);
-
- if let Some(hit) = cache.get(key) {
- hit.clone()
- } else {
- let lints = Lrc::new(run_on_chunk(linter, chunk, source));
- cache.put(key.into(), lints.clone());
- lints
+ if let Some(hit) = cache.get(chars) {
+ return hit.clone();
}
+
+ let lints = Lrc::new(run_on_chunk(linter, chunk, source));
+ cache.put(chars.into(), lints.clone());
+
+ lints
}
diff --git a/harper-core/src/linting/phrase_corrections.rs b/harper-core/src/linting/phrase_corrections.rs
index 6c969d0a5..0295cf8ac 100644
--- a/harper-core/src/linting/phrase_corrections.rs
+++ b/harper-core/src/linting/phrase_corrections.rs
@@ -1,5 +1,4 @@
use super::{LintGroup, MapPhraseLinter, PatternLinterCache};
-use std::num::NonZero;
/// Produce a [`LintGroup`] that looks for errors in common phrases.
/// Comes pre-configured with the recommended default settings.
@@ -13,14 +12,13 @@ pub fn lint_group() -> LintGroup {
$(
$group.add(
$name,
- Box::new(PatternLinterCache::new(
+ Box::new(PatternLinterCache::new_default_size(
MapPhraseLinter::new_exact_phrases(
$input,
$corrections,
$hint,
$description
),
- NonZero::new(1000).unwrap()
)),
);
)+
diff --git a/harper-core/src/linting/proper_noun_capitalization_linters.rs b/harper-core/src/linting/proper_noun_capitalization_linters.rs
index eeee428f7..bfbc2493d 100644
--- a/harper-core/src/linting/proper_noun_capitalization_linters.rs
+++ b/harper-core/src/linting/proper_noun_capitalization_linters.rs
@@ -8,7 +8,6 @@ use crate::parsers::PlainEnglish;
use crate::patterns::{ExactPhrase, Pattern, PatternMap};
use crate::{Dictionary, Document};
use crate::{Token, TokenStringExt};
-use std::num::NonZero;
use std::sync::Arc;
/// A linter that corrects the capitalization of multi-word proper nouns.
@@ -120,13 +119,12 @@ fn lint_group_from_json(json: &str, dictionary: Arc<impl Dictionary + 'static>)
for (key, rule) in rules.into_iter() {
group.add(
key,
- Box::new(PatternLinterCache::new(
+ Box::new(PatternLinterCache::new_default_size(
ProperNounCapitalizationLinter::new_strs(
rule.canonical,
rule.description,
dictionary.clone(),
),
- NonZero::new(1000).unwrap(),
)),
);
}