Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix lsp memory leak #1841

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions kclvm/sema/src/core/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ pub struct GlobalState {
pub(crate) sema_db: SemanticDB,
// new and invalidate(changed and affected by changed) pkg from CachedScope::update()
pub new_or_invalidate_pkgs: HashSet<String>,

pub ctx: GlobalStateContext,
}

#[derive(Default, Debug, Clone)]
pub struct GlobalStateContext {
pub has_init_builtin: bool,
}

impl GlobalState {
Expand Down
14 changes: 14 additions & 0 deletions kclvm/sema/src/core/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ impl SymbolData {
match id.get_kind() {
SymbolKind::Schema => {
self.schemas.remove(id.get_id());
self.symbols_info.schema_builtin_symbols.remove(id);
}
SymbolKind::Attribute => {
self.attributes.remove(id.get_id());
Expand Down Expand Up @@ -887,11 +888,21 @@ impl SymbolData {
&self.symbols_info.node_symbol_map
}

#[inline]
pub fn get_node_symbol_map_mut(&mut self) -> &mut IndexMap<NodeKey, SymbolRef> {
&mut self.symbols_info.node_symbol_map
}

#[inline]
pub fn get_symbol_node_map(&self) -> &IndexMap<SymbolRef, NodeKey> {
&self.symbols_info.symbol_node_map
}

#[inline]
pub fn get_symbol_node_map_mut(&mut self) -> &mut IndexMap<SymbolRef, NodeKey> {
&mut self.symbols_info.symbol_node_map
}

#[inline]
pub fn get_fully_qualified_name_map(&self) -> &IndexMap<String, SymbolRef> {
&self.symbols_info.fully_qualified_name_map
Expand All @@ -913,6 +924,9 @@ impl SymbolData {
}
for symbol in to_remove {
self.remove_symbol(&symbol);
if let Some(node_id) = self.get_symbol_node_map_mut().remove(&symbol) {
self.get_node_symbol_map_mut().remove(&node_id);
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion kclvm/sema/src/namer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,18 @@ impl<'ctx> Namer<'ctx> {

// serial namer pass
pub fn find_symbols(program: &'ctx Program, gs: &'ctx mut GlobalState) {
let has_init_builtin = gs.ctx.has_init_builtin;
gs.ctx.has_init_builtin = true;
let mut namer = Self::new(program, gs);
namer.ctx.current_package_info = Some(PackageInfo::new(
BUILTIN_SYMBOL_PKG_PATH.to_string(),
"".to_string(),
true,
));
namer.init_builtin_symbols();
if !has_init_builtin {
namer.init_builtin_symbols();
}

namer
.gs
.get_packages_mut()
Expand Down
11 changes: 10 additions & 1 deletion kclvm/sema/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,23 @@ pub fn resolve_program_with_opts(
cached_scope.invalidate_pkgs.clear();
cached_scope.update(program);
resolver.scope_map = cached_scope.scope_map.clone();
resolver.node_ty_map = Rc::new(RefCell::new(cached_scope.node_ty_map.clone()));
resolver.ctx.schema_mapping = cached_scope.schema_mapping.clone();
cached_scope
.invalidate_pkgs
.insert(kclvm_ast::MAIN_PKG.to_string());
for pkg in &cached_scope.invalidate_pkgs {
resolver.scope_map.remove(pkg);
}
let mut nodes = vec![];
for node in cached_scope.node_ty_map.keys() {
if cached_scope.invalidate_pkgs.contains(&node.pkgpath) {
nodes.push(node.clone());
}
}
for node in nodes {
cached_scope.node_ty_map.remove(&node);
}
resolver.node_ty_map = Rc::new(RefCell::new(cached_scope.node_ty_map.clone()));
}
}
let scope = resolver.check_and_lint_all_pkgs();
Expand Down
51 changes: 51 additions & 0 deletions kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,3 +1098,54 @@ fn test_clear_cache_by_module() {
assert_eq!(cached_scope.invalidate_pkgs, expect);
};
}

#[test]
fn clear_cache_test() {
let sess = Arc::new(ParseSession::default());
let mut program = load_program(
sess.clone(),
&["./src/resolver/test_data/cache/main.k"],
None,
None,
)
.unwrap()
.program;

let scope_cache = Arc::new(RwLock::new(CachedScope::default()));
let first_scope = resolve_program_with_opts(
&mut program,
Options {
merge_program: false,
type_erasure: false,
..Default::default()
},
Some(scope_cache.clone()),
);

let mut program = load_program(
sess.clone(),
&["./src/resolver/test_data/cache/main.k"],
None,
None,
)
.unwrap()
.program;

let second_scope = resolve_program_with_opts(
&mut program,
Options {
merge_program: false,
type_erasure: false,
..Default::default()
},
Some(scope_cache.clone()),
);
let first_node_ty_map_len = first_scope.node_ty_map.borrow().len();
let second_node_ty_map_len = second_scope.node_ty_map.borrow().len();
assert_eq!(first_node_ty_map_len, second_node_ty_map_len);
assert_eq!(first_scope.scope_map.len(), second_scope.scope_map.len());
assert_eq!(
first_scope.schema_mapping.len(),
second_scope.schema_mapping.len()
);
}
Loading