Skip to content

Commit

Permalink
feat: clear resolver cache by main pkg module
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Jun 3, 2024
1 parent c07925f commit 01ef42d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions kclvm/sema/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ pub fn resolve_program_with_opts(
resolver.scope_map.remove(kclvm_ast::MAIN_PKG);
resolver.node_ty_map = cached_scope.node_ty_map.clone();
resolver.ctx.schema_mapping = cached_scope.schema_mapping.clone();
cached_scope.invalidate_main_pkg_modules = None;
}
}
let scope = resolver.check_and_lint(kclvm_ast::MAIN_PKG);
Expand Down
43 changes: 40 additions & 3 deletions kclvm/sema/src/resolver/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ pub struct CachedScope {
pub scope_map: IndexMap<String, Rc<RefCell<Scope>>>,
pub schema_mapping: IndexMap<String, Arc<RefCell<SchemaType>>>,
pub node_ty_map: NodeTyMap,
//Specify the invalid module in the main package, used for invalidate_module().
// If it is None, all modules in the main package will be invalidated
pub invalidate_main_pkg_modules: Option<HashSet<String>>,
dependency_graph: DependencyGraph,
}

Expand All @@ -532,7 +535,11 @@ impl DependencyGraph {
self.node_map.clear();
}

pub fn update(&mut self, program: &ast::Program) -> Result<HashSet<String>, String> {
pub fn update(
&mut self,
program: &ast::Program,
invalidate_main_pkg_modules: &Option<HashSet<String>>,
) -> Result<HashSet<String>, String> {
let mut new_modules = HashMap::new();
for (pkgpath, modules) in program.pkgs.iter() {
if pkgpath == kclvm_ast::MAIN_PKG {
Expand Down Expand Up @@ -566,6 +573,30 @@ impl DependencyGraph {
}
let mut invalidated_set = HashSet::new();
if let Some(main_modules) = program.pkgs.get(kclvm_ast::MAIN_PKG) {
match invalidate_main_pkg_modules {
Some(modules) => {
for module in main_modules {
if modules.contains(&module.filename) {
let result = self.invalidate_module(module)?;
for pkg in result {
invalidated_set.insert(pkg);
}
self.remove_dependency_from_pkg(&module.filename);
self.add_new_module(module);
}
}
}
None => {
for module in main_modules {
let result = self.invalidate_module(module)?;
for pkg in result {
invalidated_set.insert(pkg);
}
self.remove_dependency_from_pkg(&module.filename);
self.add_new_module(module);
}
}
}
for module in main_modules {
let result = self.invalidate_module(module)?;
for pkg in result {
Expand Down Expand Up @@ -676,8 +707,11 @@ impl CachedScope {
node_ty_map: scope.node_ty_map.clone(),
dependency_graph: DependencyGraph::default(),
schema_mapping: scope.schema_mapping.clone(),
invalidate_main_pkg_modules: None,
};
let invalidated_pkgs = cached_scope.dependency_graph.update(program);
let invalidated_pkgs = cached_scope
.dependency_graph
.update(program, &cached_scope.invalidate_main_pkg_modules);
cached_scope.invalidate_cache(invalidated_pkgs.as_ref());
cached_scope
}
Expand All @@ -686,6 +720,7 @@ impl CachedScope {
self.scope_map.clear();
self.node_ty_map.clear();
self.dependency_graph.clear();
self.invalidate_main_pkg_modules = None;
}

pub fn invalidate_cache(&mut self, invalidated_pkgs: Result<&HashSet<String>, &String>) {
Expand All @@ -704,7 +739,9 @@ impl CachedScope {
self.clear();
self.program_root = program.root.clone();
}
let invalidated_pkgs = self.dependency_graph.update(program);
let invalidated_pkgs = self
.dependency_graph
.update(program, &self.invalidate_main_pkg_modules);
self.invalidate_cache(invalidated_pkgs.as_ref());
}
}
9 changes: 9 additions & 0 deletions kclvm/tools/src/LSP/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use parking_lot::RwLockReadGuard;
use ra_ap_vfs::{FileId, Vfs};
use serde::{de::DeserializeOwned, Serialize};

use std::collections::HashSet;
use std::fs;
use std::path::Path;

Expand Down Expand Up @@ -126,6 +127,14 @@ pub(crate) fn compile_with_params(
};
diags.extend(sess.1.borrow().diagnostics.clone());
// Resolver
if let Some(cached_scope) = params.scope_cache.as_ref() {
if let Ok(mut cached_scope) = cached_scope.try_lock() {
let mut invalidate_main_pkg_modules = HashSet::new();
invalidate_main_pkg_modules.insert(params.file);
cached_scope.invalidate_main_pkg_modules = Some(invalidate_main_pkg_modules);
}
}

let prog_scope = resolve_program_with_opts(
&mut program,
kclvm_sema::resolver::Options {
Expand Down

0 comments on commit 01ef42d

Please sign in to comment.