Skip to content

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

perf(core): cache Wier ASTs (#2584)

Steve Kurch1d6374db2c82
diff --git a/harper-core/src/weir/mod.rs b/harper-core/src/weir/mod.rs
index a9e6b1bd0..7420c9415 100644
--- a/harper-core/src/weir/mod.rs
+++ b/harper-core/src/weir/mod.rs
@@ -7,6 +7,7 @@ mod optimize;
mod parsing;
use std::str::FromStr;
+use std::sync::Arc;
pub use error::Error;
use is_macro::Is;
@@ -45,7 +46,7 @@ pub struct WeirLinter {
strategy: ReplacementStrategy,
replacements: Vec<String>,
lint_kind: LintKind,
- ast: Ast,
+ ast: Arc<Ast>,
}
impl WeirLinter {
diff --git a/harper-core/src/weir/parsing/stmt.rs b/harper-core/src/weir/parsing/stmt.rs
index f35845bbe..720df4d5b 100644
--- a/harper-core/src/weir/parsing/stmt.rs
+++ b/harper-core/src/weir/parsing/stmt.rs
@@ -1,6 +1,11 @@
+use std::num::NonZeroUsize;
+use std::sync::{Arc, LazyLock, RwLock};
+
+use lru::LruCache;
+
use super::ast::AstVariable;
use super::inc_by_spaces;
-use crate::{CharString, CharStringExt, Punctuation, Token, TokenKind, TokenStringExt};
+use crate::{CharStringExt, Punctuation, Token, TokenKind, TokenStringExt};
use super::expr::parse_seq;
use super::{
@@ -8,17 +13,48 @@ use super::{
locate_matching_brace, optimize, parse_collection,
};
-pub fn parse_str(weir_code: &str, use_optimizer: bool) -> Result<Ast, Error> {
- let chars: CharString = weir_code.chars().collect();
- let tokens = lex(&chars);
+/// Parse the provided string using caching to speed up repeated requests.
+///
+/// Will return the cached AST if the provided code has previously been cached. Otherwise, it will
+/// parse the code and cache the resulting AST.
+pub fn parse_str(weir_code: &str, use_optimizer: bool) -> Result<Arc<Ast>, Error> {
+ // The parameters that might influence the generated AST.
+ // This is used as the key for the cache hashmap.
+ type ParseStrParams = (Arc<String>, bool);
- let mut stmts = parse_stmt_list(&tokens, &chars)?;
+ static PARSE_CACHE: LazyLock<
+ RwLock<LruCache<ParseStrParams, Arc<Ast>, hashbrown::DefaultHashBuilder>>,
+ > = LazyLock::new(|| RwLock::new(LruCache::new(NonZeroUsize::new(10000).unwrap())));
- if use_optimizer {
- while optimize(&mut stmts) {}
- }
+ let weir_code = Arc::new(weir_code.to_owned());
+
+ Ok(
+ if let Some(cached) = PARSE_CACHE
+ .read()
+ .unwrap()
+ .peek(&(weir_code.clone(), use_optimizer))
+ {
+ cached.clone()
+ } else {
+ let chars: Vec<char> = weir_code.chars().collect();
+ let tokens = lex(&chars);
+
+ let mut stmts = parse_stmt_list(&tokens, &chars)?;
+
+ if use_optimizer {
+ while optimize(&mut stmts) {}
+ }
+
+ let ast = Arc::new(Ast { stmts });
+
+ PARSE_CACHE
+ .write()
+ .unwrap()
+ .put((weir_code, use_optimizer), ast.clone());
- Ok(Ast { stmts })
+ ast
+ },
+ )
}
fn parse_stmt_list(tokens: &[Token], source: &[char]) -> Result<Vec<AstStmtNode>, Error> {