use ra_db::{
salsa::{Database, Durability, SweepStrategy},
- CrateGraph, FileId, RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot,
- SourceRootId,
+ CrateGraph, FileId, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId,
};
use ra_prof::{memory_usage, profile, Bytes};
-use rustc_hash::FxHashMap;
+use rustc_hash::FxHashSet;
use crate::{symbol_index::SymbolsDatabase, RootDatabase};
#[derive(Default)]
pub struct AnalysisChange {
- new_roots: Vec<(SourceRootId, bool)>,
- roots_changed: FxHashMap<SourceRootId, RootChange>,
- files_changed: Vec<(FileId, Arc<String>)>,
+ roots: Option<Vec<SourceRoot>>,
+ files_changed: Vec<(FileId, Option<Arc<String>>)>,
crate_graph: Option<CrateGraph>,
}
impl fmt::Debug for AnalysisChange {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let mut d = fmt.debug_struct("AnalysisChange");
- if !self.new_roots.is_empty() {
- d.field("new_roots", &self.new_roots);
- }
- if !self.roots_changed.is_empty() {
- d.field("roots_changed", &self.roots_changed);
+ if let Some(roots) = &self.roots {
+ d.field("roots", roots);
}
if !self.files_changed.is_empty() {
d.field("files_changed", &self.files_changed.len());
AnalysisChange::default()
}
- pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) {
- self.new_roots.push((root_id, is_local));
- }
-
- pub fn add_file(
- &mut self,
- root_id: SourceRootId,
- file_id: FileId,
- path: RelativePathBuf,
- text: Arc<String>,
- ) {
- let file = AddFile { file_id, path, text };
- self.roots_changed.entry(root_id).or_default().added.push(file);
+ pub fn set_roots(&mut self, roots: Vec<SourceRoot>) {
+ self.roots = Some(roots);
}
- pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) {
+ pub fn change_file(&mut self, file_id: FileId, new_text: Option<Arc<String>>) {
self.files_changed.push((file_id, new_text))
}
- pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) {
- let file = RemoveFile { file_id, path };
- self.roots_changed.entry(root_id).or_default().removed.push(file);
- }
-
pub fn set_crate_graph(&mut self, graph: CrateGraph) {
self.crate_graph = Some(graph);
}
#[derive(Debug)]
struct AddFile {
file_id: FileId,
- path: RelativePathBuf,
+ path: String,
text: Arc<String>,
}
#[derive(Debug)]
struct RemoveFile {
file_id: FileId,
- path: RelativePathBuf,
+ path: String,
}
#[derive(Default)]
let _p = profile("RootDatabase::apply_change");
self.request_cancellation();
log::info!("apply_change {:?}", change);
- if !change.new_roots.is_empty() {
- let mut local_roots = Vec::clone(&self.local_roots());
- let mut libraries = Vec::clone(&self.library_roots());
- for (root_id, is_local) in change.new_roots {
- let root =
- if is_local { SourceRoot::new_local() } else { SourceRoot::new_library() };
+ if let Some(roots) = change.roots {
+ let mut local_roots = FxHashSet::default();
+ let mut library_roots = FxHashSet::default();
+ for (idx, root) in roots.into_iter().enumerate() {
+ let root_id = SourceRootId(idx as u32);
let durability = durability(&root);
- self.set_source_root_with_durability(root_id, Arc::new(root), durability);
- if is_local {
- local_roots.push(root_id);
+ if root.is_library {
+ library_roots.insert(root_id);
} else {
- libraries.push(root_id)
+ local_roots.insert(root_id);
+ }
+ for file_id in root.iter() {
+ self.set_file_source_root_with_durability(file_id, root_id, durability);
}
+ self.set_source_root_with_durability(root_id, Arc::new(root), durability);
}
self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
- self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH);
+ self.set_library_roots_with_durability(Arc::new(library_roots), Durability::HIGH);
}
- for (root_id, root_change) in change.roots_changed {
- self.apply_root_change(root_id, root_change);
- }
for (file_id, text) in change.files_changed {
let source_root_id = self.file_source_root(file_id);
let source_root = self.source_root(source_root_id);
let durability = durability(&source_root);
+ // XXX: can't actually remove the file, just reset the text
+ let text = text.unwrap_or_default();
self.set_file_text_with_durability(file_id, text, durability)
}
if let Some(crate_graph) = change.crate_graph {
}
}
- fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) {
- let mut source_root = SourceRoot::clone(&self.source_root(root_id));
- let durability = durability(&source_root);
- for add_file in root_change.added {
- self.set_file_text_with_durability(add_file.file_id, add_file.text, durability);
- self.set_file_relative_path_with_durability(
- add_file.file_id,
- add_file.path.clone(),
- durability,
- );
- self.set_file_source_root_with_durability(add_file.file_id, root_id, durability);
- source_root.insert_file(add_file.path, add_file.file_id);
- }
- for remove_file in root_change.removed {
- self.set_file_text_with_durability(remove_file.file_id, Default::default(), durability);
- source_root.remove_file(&remove_file.path);
- }
- self.set_source_root_with_durability(root_id, Arc::new(source_root), durability);
- }
-
pub fn maybe_collect_garbage(&mut self) {
if cfg!(feature = "wasm") {
return;
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
- self.query(ra_db::ParseQuery).sweep(sweep);
- self.query(hir::db::ParseMacroQuery).sweep(sweep);
+ ra_db::ParseQuery.in_db(self).sweep(sweep);
+ hir::db::ParseMacroQuery.in_db(self).sweep(sweep);
// Macros do take significant space, but less then the syntax trees
// self.query(hir::db::MacroDefQuery).sweep(sweep);
// self.query(hir::db::MacroArgQuery).sweep(sweep);
// self.query(hir::db::MacroExpandQuery).sweep(sweep);
- self.query(hir::db::AstIdMapQuery).sweep(sweep);
+ hir::db::AstIdMapQuery.in_db(self).sweep(sweep);
- self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep);
+ hir::db::BodyWithSourceMapQuery.in_db(self).sweep(sweep);
- self.query(hir::db::ExprScopesQuery).sweep(sweep);
- self.query(hir::db::InferQueryQuery).sweep(sweep);
- self.query(hir::db::BodyQuery).sweep(sweep);
+ hir::db::ExprScopesQuery.in_db(self).sweep(sweep);
+ hir::db::InferQueryQuery.in_db(self).sweep(sweep);
+ hir::db::BodyQuery.in_db(self).sweep(sweep);
}
+ // Feature: Memory Usage
+ //
+ // Clears rust-analyzer's internal database and prints memory usage statistics.
+ //
+ // |===
+ // | Editor | Action Name
+ //
+ // | VS Code | **Rust Analyzer: Memory Usage (Clears Database)**
+ // |===
pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> {
let mut acc: Vec<(String, Bytes)> = vec![];
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
macro_rules! sweep_each_query {
($($q:path)*) => {$(
let before = memory_usage().allocated;
- self.query($q).sweep(sweep);
+ $q.in_db(self).sweep(sweep);
let after = memory_usage().allocated;
let q: $q = Default::default();
let name = format!("{:?}", q);
acc.push((name, before - after));
let before = memory_usage().allocated;
- self.query($q).sweep(sweep.discard_everything());
+ $q.in_db(self).sweep(sweep.discard_everything());
let after = memory_usage().allocated;
let q: $q = Default::default();
let name = format!("{:?} (deps)", q);
// AstDatabase
hir::db::AstIdMapQuery
- hir::db::InternMacroQuery
hir::db::MacroArgQuery
hir::db::MacroDefQuery
hir::db::ParseMacroQuery
hir::db::MacroExpandQuery
- hir::db::InternEagerExpansionQuery
// DefDatabase
- hir::db::RawItemsQuery
+ hir::db::ItemTreeQuery
hir::db::CrateDefMapQueryQuery
hir::db::StructDataQuery
hir::db::UnionDataQuery
hir::db::DocumentationQuery
hir::db::ImportMapQuery
- // InternDatabase
- hir::db::InternFunctionQuery
- hir::db::InternStructQuery
- hir::db::InternUnionQuery
- hir::db::InternEnumQuery
- hir::db::InternConstQuery
- hir::db::InternStaticQuery
- hir::db::InternTraitQuery
- hir::db::InternTypeAliasQuery
- hir::db::InternImplQuery
-
// HirDatabase
hir::db::InferQueryQuery
hir::db::TyQuery
hir::db::GenericPredicatesForParamQuery
hir::db::GenericPredicatesQuery
hir::db::GenericDefaultsQuery
- hir::db::ImplsInCrateQuery
- hir::db::ImplsFromDepsQuery
- hir::db::InternTypeCtorQuery
- hir::db::InternTypeParamIdQuery
- hir::db::InternChalkImplQuery
- hir::db::InternAssocTyValueQuery
+ hir::db::InherentImplsInCrateQuery
+ hir::db::TraitImplsInCrateQuery
+ hir::db::TraitImplsInDepsQuery
hir::db::AssociatedTyDataQuery
hir::db::TraitDatumQuery
hir::db::StructDatumQuery
// LineIndexDatabase
crate::LineIndexQuery
];
+
+ // To collect interned data, we need to bump the revision counter by performing a synthetic
+ // write.
+ // We do this after collecting the non-interned queries to correctly attribute memory used
+ // by interned data.
+ self.salsa_runtime_mut().synthetic_write(Durability::HIGH);
+
+ sweep_each_query![
+ // AstDatabase
+ hir::db::InternMacroQuery
+ hir::db::InternEagerExpansionQuery
+
+ // InternDatabase
+ hir::db::InternFunctionQuery
+ hir::db::InternStructQuery
+ hir::db::InternUnionQuery
+ hir::db::InternEnumQuery
+ hir::db::InternConstQuery
+ hir::db::InternStaticQuery
+ hir::db::InternTraitQuery
+ hir::db::InternTypeAliasQuery
+ hir::db::InternImplQuery
+
+ // HirDatabase
+ hir::db::InternTypeCtorQuery
+ hir::db::InternTypeParamIdQuery
+ hir::db::InternChalkImplQuery
+ hir::db::InternAssocTyValueQuery
+ ];
+
acc.sort_by_key(|it| std::cmp::Reverse(it.1));
acc
}