From 7801f6b50f69b33743145f61989bcb945c24dbaa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 26 Jan 2019 20:33:33 +0300 Subject: automatically collect garbage --- crates/ra_ide_api/src/imp.rs | 48 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'crates/ra_ide_api/src/imp.rs') diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs index 399433a01..0dd3e8cb0 100644 --- a/crates/ra_ide_api/src/imp.rs +++ b/crates/ra_ide_api/src/imp.rs @@ -1,4 +1,7 @@ -use std::sync::Arc; +use std::{ + sync::Arc, + time, +}; use hir::{ self, Problem, source_binder @@ -19,12 +22,14 @@ use crate::{ CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit, Query, RootChange, SourceChange, SourceFileEdit, symbol_index::{FileSymbol, SymbolsDatabase}, + status::syntax_tree_stats }; +const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100); + impl db::RootDatabase { pub(crate) fn apply_change(&mut self, change: AnalysisChange) { log::info!("apply_change {:?}", change); - // self.gc_syntax_trees(); if !change.new_roots.is_empty() { let mut local_roots = Vec::clone(&self.local_roots()); for (root_id, is_local) in change.new_roots { @@ -72,18 +77,41 @@ impl db::RootDatabase { self.set_source_root(root_id, Arc::new(source_root)); } + pub(crate) fn maybe_collect_garbage(&mut self) { + if self.last_gc_check.elapsed() > GC_COOLDOWN { + self.last_gc_check = time::Instant::now(); + let retained_trees = syntax_tree_stats(self).retained; + if retained_trees > 100 { + log::info!( + "automatic garbadge collection, {} retained trees", + retained_trees + ); + self.collect_garbage(); + } + } + } + /// Ideally, we should call this function from time to time to collect heavy /// syntax trees. However, if we actually do that, everything is recomputed /// for some reason. Needs investigation. pub(crate) fn collect_garbage(&mut self) { - self.query(ra_db::ParseQuery) - .sweep(SweepStrategy::default().discard_values()); - self.query(hir::db::HirParseQuery) - .sweep(SweepStrategy::default().discard_values()); - self.query(hir::db::FileItemsQuery) - .sweep(SweepStrategy::default().discard_values()); - self.query(hir::db::FileItemQuery) - .sweep(SweepStrategy::default().discard_values()); + self.last_gc = time::Instant::now(); + + let sweep = SweepStrategy::default() + .discard_values() + .discard_all_revisions(); + + self.query(ra_db::ParseQuery).sweep(sweep.clone()); + + self.query(hir::db::HirParseQuery).sweep(sweep.clone()); + self.query(hir::db::FileItemsQuery).sweep(sweep.clone()); + self.query(hir::db::FileItemQuery).sweep(sweep.clone()); + + self.query(hir::db::LowerModuleQuery).sweep(sweep.clone()); + self.query(hir::db::LowerModuleSourceMapQuery) + .sweep(sweep.clone()); + self.query(hir::db::BodySyntaxMappingQuery) + .sweep(sweep.clone()); } } -- cgit v1.2.3