Skip to content

Commit

Permalink
preprocessor - cache file tokenization for common includes (#901)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson authored Feb 27, 2025
1 parent 6a310d0 commit e569bde
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ byteorder = "1.5.0"
chumsky = "0.9.3"
clap = "4.5.30"
codespan-reporting = { version = "0.11.1", features = ["serialization"] }
dashmap = "6.1.0"
dirs = "6.0.0"
git2 = "0.20.0"
indexmap = "2.7.1"
Expand Down
4 changes: 2 additions & 2 deletions hls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ edition = "2024"
[dependencies]
hemtt-common = { path = "../libs/common" }
hemtt-config = { path = "../libs/config" }
hemtt-preprocessor = { path = "../libs/preprocessor" }
hemtt-preprocessor = { path = "../libs/preprocessor", features = ["lsp"] }
hemtt-sqf = { path = "../libs/sqf" }
hemtt-workspace = { path = "../libs/workspace" }

arma3-wiki = { workspace = true }
chumsky = { workspace = true }
clap = { workspace = true, features = ["derive"] }
dashmap = "6.1.0"
dashmap = { workspace = true }
regex = { workspace = true }
ropey = "1.6.1"
serde = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions libs/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ bench = false

[dependencies]
hemtt-common = { path = "../common", version = "1.0.0" }
hemtt-preprocessor = { path = "../preprocessor", version = "1.0.0", features = ["lsp"] }
hemtt-workspace = { path = "../workspace", version = "1.0.0", features = ["lsp"] }
hemtt-preprocessor = { path = "../preprocessor", version = "1.0.0" }
hemtt-workspace = { path = "../workspace", version = "1.0.0" }

automod = { workspace = true }
byteorder = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions libs/preprocessor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ hemtt-common = { path = "../common", version = "1.0.0" }
hemtt-workspace = { path = "../workspace", version = "1.0.0" }

automod = { workspace = true }
dashmap = { workspace = true }
lsp-types = { workspace = true, optional = true }
peekmore = { workspace = true }
pest = { workspace = true }
Expand Down
38 changes: 35 additions & 3 deletions libs/preprocessor/src/parse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use dashmap::DashMap;
use hemtt_workspace::{
WorkspacePath,
position::{LineCol, Position},
Expand All @@ -16,6 +17,20 @@ use crate::{Error, codes::pe24_parsing_failed::ParsingFailed};
/// Parser for the preprocessor, generated from `config.pest`
pub struct PreprocessorParser;

#[derive(Debug, Clone)]
struct Cache {
tokens: Arc<DashMap<WorkspacePath, Vec<Arc<Token>>>>,
}

impl Cache {
pub fn get() -> Self {
static SINGLETON: LazyLock<Cache> = LazyLock::new(|| Cache {
tokens: Arc::new(DashMap::new()),
});
(*SINGLETON).clone()
}
}

/// Parse a file into tokens
///
/// # Errors
Expand All @@ -24,14 +39,31 @@ pub struct PreprocessorParser;
/// # Panics
/// If the file is invalid
pub fn file(path: &WorkspacePath) -> Result<Vec<Arc<Token>>, Error> {
let cache = Cache::get();
if cache.tokens.contains_key(path) {
return Ok(cache.tokens.get(path).expect("exists").value().clone());
}
let source = path.read_to_string()?;
str(&source, path)
let res = str(&source, path)?;
// The LSP manages its own caches, having this enabled would cause the LSP to never see any changes
// This will make the LSP slower, in the future it could have a way to invalidate the cache (when a file is saved)
#[cfg(not(feature = "lsp"))]
{
let path_str = path.as_str();
if ["macros", "common", "script", "component"]
.iter()
.any(|&x| path_str.contains(x))
{
cache.tokens.insert(path.clone(), res.clone());
}
}
Ok(res)
}

/// Parse a string into tokens
///
/// # Errors
/// If the string is invalid
/// If the string is invalid1
///
/// # Panics
/// If the string is invalid
Expand Down

0 comments on commit e569bde

Please sign in to comment.