aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/ide_db/change.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/ide_db/change.rs')
-rw-r--r--crates/ra_ide/src/ide_db/change.rs386
1 files changed, 0 insertions, 386 deletions
diff --git a/crates/ra_ide/src/ide_db/change.rs b/crates/ra_ide/src/ide_db/change.rs
deleted file mode 100644
index 62ffa6920..000000000
--- a/crates/ra_ide/src/ide_db/change.rs
+++ /dev/null
@@ -1,386 +0,0 @@
1//! FIXME: write short doc here
2
3use std::{fmt, sync::Arc, time};
4
5use ra_db::{
6 salsa::{Database, Durability, SweepStrategy},
7 CrateGraph, CrateId, FileId, RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot,
8 SourceRootId,
9};
10use ra_prof::{memory_usage, profile, Bytes};
11use ra_syntax::SourceFile;
12#[cfg(not(feature = "wasm"))]
13use rayon::prelude::*;
14use rustc_hash::FxHashMap;
15
16use crate::ide_db::{
17 symbol_index::{SymbolIndex, SymbolsDatabase},
18 DebugData, RootDatabase,
19};
20
21#[derive(Default)]
22pub 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,
29}
30
31impl 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);
36 }
37 if !self.roots_changed.is_empty() {
38 d.field("roots_changed", &self.roots_changed);
39 }
40 if !self.files_changed.is_empty() {
41 d.field("files_changed", &self.files_changed.len());
42 }
43 if !self.libraries_added.is_empty() {
44 d.field("libraries_added", &self.libraries_added.len());
45 }
46 if !self.crate_graph.is_none() {
47 d.field("crate_graph", &self.crate_graph);
48 }
49 d.finish()
50 }
51}
52
53impl AnalysisChange {
54 pub fn new() -> AnalysisChange {
55 AnalysisChange::default()
56 }
57
58 pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) {
59 self.new_roots.push((root_id, is_local));
60 }
61
62 pub fn add_file(
63 &mut self,
64 root_id: SourceRootId,
65 file_id: FileId,
66 path: RelativePathBuf,
67 text: Arc<String>,
68 ) {
69 let file = AddFile { file_id, path, text };
70 self.roots_changed.entry(root_id).or_default().added.push(file);
71 }
72
73 pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) {
74 self.files_changed.push((file_id, new_text))
75 }
76
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);
80 }
81
82 pub fn add_library(&mut self, data: LibraryData) {
83 self.libraries_added.push(data)
84 }
85
86 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
87 self.crate_graph = Some(graph);
88 }
89
90 pub fn set_debug_crate_name(&mut self, crate_id: CrateId, name: String) {
91 self.debug_data.crate_names.insert(crate_id, name);
92 }
93
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);
96 }
97}
98
99#[derive(Debug)]
100struct AddFile {
101 file_id: FileId,
102 path: RelativePathBuf,
103 text: Arc<String>,
104}
105
106#[derive(Debug)]
107struct RemoveFile {
108 file_id: FileId,
109 path: RelativePathBuf,
110}
111
112#[derive(Default)]
113struct RootChange {
114 added: Vec<AddFile>,
115 removed: Vec<RemoveFile>,
116}
117
118impl 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())
123 .finish()
124 }
125}
126
127pub struct LibraryData {
128 root_id: SourceRootId,
129 root_change: RootChange,
130 symbol_index: SymbolIndex,
131}
132
133impl 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())
139 .finish()
140 }
141}
142
143impl LibraryData {
144 pub fn prepare(
145 root_id: SourceRootId,
146 files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
147 ) -> LibraryData {
148 let _p = profile("LibraryData::prepare");
149
150 #[cfg(not(feature = "wasm"))]
151 let iter = files.par_iter();
152 #[cfg(feature = "wasm")]
153 let iter = files.iter();
154
155 let symbol_index = SymbolIndex::for_files(iter.map(|(file_id, _, text)| {
156 let parse = SourceFile::parse(text);
157 (*file_id, parse)
158 }));
159 let mut root_change = RootChange::default();
160 root_change.added = files
161 .into_iter()
162 .map(|(file_id, path, text)| AddFile { file_id, path, text })
163 .collect();
164 LibraryData { root_id, root_change, symbol_index }
165 }
166}
167
168const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100);
169
170impl RootDatabase {
171 pub(crate) fn request_cancellation(&mut self) {
172 let _p = profile("RootDatabase::request_cancellation");
173 self.salsa_runtime_mut().synthetic_write(Durability::LOW);
174 }
175
176 pub(crate) 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 {
183 let root =
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);
187 if is_local {
188 local_roots.push(root_id);
189 }
190 }
191 self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
192 }
193
194 for (root_id, root_change) in change.roots_changed {
195 self.apply_root_change(root_id, root_change);
196 }
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)
202 }
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(
208 library.root_id,
209 Arc::new(SourceRoot::new_library()),
210 Durability::HIGH,
211 );
212 self.set_library_symbols_with_durability(
213 library.root_id,
214 Arc::new(library.symbol_index),
215 Durability::HIGH,
216 );
217 self.apply_root_change(library.root_id, library.root_change);
218 }
219 self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH);
220 }
221 if let Some(crate_graph) = change.crate_graph {
222 self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH)
223 }
224
225 Arc::make_mut(&mut self.debug_data).merge(change.debug_data)
226 }
227
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(
234 add_file.file_id,
235 add_file.path.clone(),
236 durability,
237 );
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);
240 }
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);
244 }
245 self.set_source_root_with_durability(root_id, Arc::new(source_root), durability);
246 }
247
248 pub(crate) fn maybe_collect_garbage(&mut self) {
249 if cfg!(feature = "wasm") {
250 return;
251 }
252
253 if self.last_gc_check.elapsed() > GC_COOLDOWN {
254 self.last_gc_check = crate::wasm_shims::Instant::now();
255 }
256 }
257
258 pub(crate) fn collect_garbage(&mut self) {
259 if cfg!(feature = "wasm") {
260 return;
261 }
262
263 let _p = profile("RootDatabase::collect_garbage");
264 self.last_gc = crate::wasm_shims::Instant::now();
265
266 let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
267
268 self.query(ra_db::ParseQuery).sweep(sweep);
269 self.query(hir::db::ParseMacroQuery).sweep(sweep);
270
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);
275
276 self.query(hir::db::AstIdMapQuery).sweep(sweep);
277
278 self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep);
279
280 self.query(hir::db::ExprScopesQuery).sweep(sweep);
281 self.query(hir::db::DoInferQuery).sweep(sweep);
282 self.query(hir::db::BodyQuery).sweep(sweep);
283 }
284
285 pub(crate) 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 {
289 ($($q:path)*) => {$(
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));
296
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));
303 )*}
304 }
305 sweep_each_query![
306 // SourceDatabase
307 ra_db::ParseQuery
308 ra_db::SourceRootCratesQuery
309
310 // AstDatabase
311 hir::db::AstIdMapQuery
312 hir::db::InternMacroQuery
313 hir::db::MacroArgQuery
314 hir::db::MacroDefQuery
315 hir::db::ParseMacroQuery
316 hir::db::MacroExpandQuery
317
318 // DefDatabase
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
331 hir::db::BodyQuery
332 hir::db::ExprScopesQuery
333 hir::db::GenericParamsQuery
334 hir::db::AttrsQuery
335 hir::db::ModuleLangItemsQuery
336 hir::db::CrateLangItemsQuery
337 hir::db::LangItemQuery
338 hir::db::DocumentationQuery
339
340 // InternDatabase
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
350
351 // HirDatabase
352 hir::db::DoInferQuery
353 hir::db::TyQuery
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
374 ];
375 acc.sort_by_key(|it| std::cmp::Reverse(it.1));
376 acc
377 }
378}
379
380fn durability(source_root: &SourceRoot) -> Durability {
381 if source_root.is_library {
382 Durability::HIGH
383 } else {
384 Durability::LOW
385 }
386}