1 //! FIXME: write short doc here
3 use std::{fmt, sync::Arc, time};
6 salsa::{Database, Durability, SweepStrategy},
7 CrateGraph, CrateId, FileId, RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot,
10 use ra_prof::{memory_usage, profile, Bytes};
11 use ra_syntax::SourceFile;
12 #[cfg(not(feature = "wasm"))]
13 use rayon::prelude::*;
14 use rustc_hash::FxHashMap;
17 symbol_index::{SymbolIndex, SymbolsDatabase},
18 DebugData, RootDatabase,
22 pub struct AnalysisChange {
23 new_roots: Vec<(SourceRootId, bool)>,
24 roots_changed: FxHashMap<SourceRootId, RootChange>,
25 files_changed: Vec<(FileId, Arc<String>)>,
26 libraries_added: Vec<LibraryData>,
27 crate_graph: Option<CrateGraph>,
28 debug_data: DebugData,
31 impl fmt::Debug for AnalysisChange {
32 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
33 let mut d = fmt.debug_struct("AnalysisChange");
34 if !self.new_roots.is_empty() {
35 d.field("new_roots", &self.new_roots);
37 if !self.roots_changed.is_empty() {
38 d.field("roots_changed", &self.roots_changed);
40 if !self.files_changed.is_empty() {
41 d.field("files_changed", &self.files_changed.len());
43 if !self.libraries_added.is_empty() {
44 d.field("libraries_added", &self.libraries_added.len());
46 if !self.crate_graph.is_none() {
47 d.field("crate_graph", &self.crate_graph);
54 pub fn new() -> AnalysisChange {
55 AnalysisChange::default()
58 pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) {
59 self.new_roots.push((root_id, is_local));
64 root_id: SourceRootId,
66 path: RelativePathBuf,
69 let file = AddFile { file_id, path, text };
70 self.roots_changed.entry(root_id).or_default().added.push(file);
73 pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) {
74 self.files_changed.push((file_id, new_text))
77 pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) {
78 let file = RemoveFile { file_id, path };
79 self.roots_changed.entry(root_id).or_default().removed.push(file);
82 pub fn add_library(&mut self, data: LibraryData) {
83 self.libraries_added.push(data)
86 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
87 self.crate_graph = Some(graph);
90 pub fn set_debug_crate_name(&mut self, crate_id: CrateId, name: String) {
91 self.debug_data.crate_names.insert(crate_id, name);
94 pub fn set_debug_root_path(&mut self, source_root_id: SourceRootId, path: String) {
95 self.debug_data.root_paths.insert(source_root_id, path);
102 path: RelativePathBuf,
109 path: RelativePathBuf,
115 removed: Vec<RemoveFile>,
118 impl fmt::Debug for RootChange {
119 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
120 fmt.debug_struct("AnalysisChange")
121 .field("added", &self.added.len())
122 .field("removed", &self.removed.len())
127 pub struct LibraryData {
128 root_id: SourceRootId,
129 root_change: RootChange,
130 symbol_index: SymbolIndex,
133 impl fmt::Debug for LibraryData {
134 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135 f.debug_struct("LibraryData")
136 .field("root_id", &self.root_id)
137 .field("root_change", &self.root_change)
138 .field("n_symbols", &self.symbol_index.len())
145 root_id: SourceRootId,
146 files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
148 let _p = profile("LibraryData::prepare");
150 #[cfg(not(feature = "wasm"))]
151 let iter = files.par_iter();
152 #[cfg(feature = "wasm")]
153 let iter = files.iter();
155 let symbol_index = SymbolIndex::for_files(iter.map(|(file_id, _, text)| {
156 let parse = SourceFile::parse(text);
159 let mut root_change = RootChange::default();
160 root_change.added = files
162 .map(|(file_id, path, text)| AddFile { file_id, path, text })
164 LibraryData { root_id, root_change, symbol_index }
168 const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100);
171 pub fn request_cancellation(&mut self) {
172 let _p = profile("RootDatabase::request_cancellation");
173 self.salsa_runtime_mut().synthetic_write(Durability::LOW);
176 pub fn apply_change(&mut self, change: AnalysisChange) {
177 let _p = profile("RootDatabase::apply_change");
178 self.request_cancellation();
179 log::info!("apply_change {:?}", change);
180 if !change.new_roots.is_empty() {
181 let mut local_roots = Vec::clone(&self.local_roots());
182 for (root_id, is_local) in change.new_roots {
184 if is_local { SourceRoot::new_local() } else { SourceRoot::new_library() };
185 let durability = durability(&root);
186 self.set_source_root_with_durability(root_id, Arc::new(root), durability);
188 local_roots.push(root_id);
191 self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
194 for (root_id, root_change) in change.roots_changed {
195 self.apply_root_change(root_id, root_change);
197 for (file_id, text) in change.files_changed {
198 let source_root_id = self.file_source_root(file_id);
199 let source_root = self.source_root(source_root_id);
200 let durability = durability(&source_root);
201 self.set_file_text_with_durability(file_id, text, durability)
203 if !change.libraries_added.is_empty() {
204 let mut libraries = Vec::clone(&self.library_roots());
205 for library in change.libraries_added {
206 libraries.push(library.root_id);
207 self.set_source_root_with_durability(
209 Arc::new(SourceRoot::new_library()),
212 self.set_library_symbols_with_durability(
214 Arc::new(library.symbol_index),
217 self.apply_root_change(library.root_id, library.root_change);
219 self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH);
221 if let Some(crate_graph) = change.crate_graph {
222 self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH)
225 Arc::make_mut(&mut self.debug_data).merge(change.debug_data)
228 fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) {
229 let mut source_root = SourceRoot::clone(&self.source_root(root_id));
230 let durability = durability(&source_root);
231 for add_file in root_change.added {
232 self.set_file_text_with_durability(add_file.file_id, add_file.text, durability);
233 self.set_file_relative_path_with_durability(
235 add_file.path.clone(),
238 self.set_file_source_root_with_durability(add_file.file_id, root_id, durability);
239 source_root.insert_file(add_file.path, add_file.file_id);
241 for remove_file in root_change.removed {
242 self.set_file_text_with_durability(remove_file.file_id, Default::default(), durability);
243 source_root.remove_file(&remove_file.path);
245 self.set_source_root_with_durability(root_id, Arc::new(source_root), durability);
248 pub fn maybe_collect_garbage(&mut self) {
249 if cfg!(feature = "wasm") {
253 if self.last_gc_check.elapsed() > GC_COOLDOWN {
254 self.last_gc_check = crate::wasm_shims::Instant::now();
258 pub fn collect_garbage(&mut self) {
259 if cfg!(feature = "wasm") {
263 let _p = profile("RootDatabase::collect_garbage");
264 self.last_gc = crate::wasm_shims::Instant::now();
266 let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
268 self.query(ra_db::ParseQuery).sweep(sweep);
269 self.query(hir::db::ParseMacroQuery).sweep(sweep);
271 // Macros do take significant space, but less then the syntax trees
272 // self.query(hir::db::MacroDefQuery).sweep(sweep);
273 // self.query(hir::db::MacroArgQuery).sweep(sweep);
274 // self.query(hir::db::MacroExpandQuery).sweep(sweep);
276 self.query(hir::db::AstIdMapQuery).sweep(sweep);
278 self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep);
280 self.query(hir::db::ExprScopesQuery).sweep(sweep);
281 self.query(hir::db::DoInferQuery).sweep(sweep);
282 self.query(hir::db::BodyQuery).sweep(sweep);
285 pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> {
286 let mut acc: Vec<(String, Bytes)> = vec![];
287 let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
288 macro_rules! sweep_each_query {
290 let before = memory_usage().allocated;
291 self.query($q).sweep(sweep);
292 let after = memory_usage().allocated;
293 let q: $q = Default::default();
294 let name = format!("{:?}", q);
295 acc.push((name, before - after));
297 let before = memory_usage().allocated;
298 self.query($q).sweep(sweep.discard_everything());
299 let after = memory_usage().allocated;
300 let q: $q = Default::default();
301 let name = format!("{:?} (deps)", q);
302 acc.push((name, before - after));
308 ra_db::SourceRootCratesQuery
311 hir::db::AstIdMapQuery
312 hir::db::InternMacroQuery
313 hir::db::MacroArgQuery
314 hir::db::MacroDefQuery
315 hir::db::ParseMacroQuery
316 hir::db::MacroExpandQuery
319 hir::db::RawItemsQuery
320 hir::db::ComputeCrateDefMapQuery
321 hir::db::StructDataQuery
322 hir::db::UnionDataQuery
323 hir::db::EnumDataQuery
324 hir::db::ImplDataQuery
325 hir::db::TraitDataQuery
326 hir::db::TypeAliasDataQuery
327 hir::db::FunctionDataQuery
328 hir::db::ConstDataQuery
329 hir::db::StaticDataQuery
330 hir::db::BodyWithSourceMapQuery
332 hir::db::ExprScopesQuery
333 hir::db::GenericParamsQuery
335 hir::db::ModuleLangItemsQuery
336 hir::db::CrateLangItemsQuery
337 hir::db::LangItemQuery
338 hir::db::DocumentationQuery
341 hir::db::InternFunctionQuery
342 hir::db::InternStructQuery
343 hir::db::InternUnionQuery
344 hir::db::InternEnumQuery
345 hir::db::InternConstQuery
346 hir::db::InternStaticQuery
347 hir::db::InternTraitQuery
348 hir::db::InternTypeAliasQuery
349 hir::db::InternImplQuery
352 hir::db::DoInferQuery
354 hir::db::ValueTyQuery
355 hir::db::ImplSelfTyQuery
356 hir::db::ImplTraitQuery
357 hir::db::FieldTypesQuery
358 hir::db::CallableItemSignatureQuery
359 hir::db::GenericPredicatesForParamQuery
360 hir::db::GenericPredicatesQuery
361 hir::db::GenericDefaultsQuery
362 hir::db::ImplsInCrateQuery
363 hir::db::ImplsForTraitQuery
364 hir::db::TraitSolverQuery
365 hir::db::InternTypeCtorQuery
366 hir::db::InternChalkImplQuery
367 hir::db::InternAssocTyValueQuery
368 hir::db::AssociatedTyDataQuery
369 hir::db::AssociatedTyValueQuery
370 hir::db::TraitSolveQuery
371 hir::db::TraitDatumQuery
372 hir::db::StructDatumQuery
373 hir::db::ImplDatumQuery
375 acc.sort_by_key(|it| std::cmp::Reverse(it.1));
380 fn durability(source_root: &SourceRoot) -> Durability {
381 if source_root.is_library {