diff options
Diffstat (limited to 'crates/ra_ide_db/src/change.rs')
-rw-r--r-- | crates/ra_ide_db/src/change.rs | 387 |
1 files changed, 387 insertions, 0 deletions
diff --git a/crates/ra_ide_db/src/change.rs b/crates/ra_ide_db/src/change.rs new file mode 100644 index 000000000..4668784d3 --- /dev/null +++ b/crates/ra_ide_db/src/change.rs | |||
@@ -0,0 +1,387 @@ | |||
1 | //! Defines a unit of change that can applied to a state of IDE to get the next | ||
2 | //! state. Changes are transactional. | ||
3 | |||
4 | use std::{fmt, sync::Arc, time}; | ||
5 | |||
6 | use ra_db::{ | ||
7 | salsa::{Database, Durability, SweepStrategy}, | ||
8 | CrateGraph, CrateId, FileId, RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot, | ||
9 | SourceRootId, | ||
10 | }; | ||
11 | use ra_prof::{memory_usage, profile, Bytes}; | ||
12 | use ra_syntax::SourceFile; | ||
13 | #[cfg(not(feature = "wasm"))] | ||
14 | use rayon::prelude::*; | ||
15 | use rustc_hash::FxHashMap; | ||
16 | |||
17 | use crate::{ | ||
18 | symbol_index::{SymbolIndex, SymbolsDatabase}, | ||
19 | DebugData, RootDatabase, | ||
20 | }; | ||
21 | |||
22 | #[derive(Default)] | ||
23 | pub struct AnalysisChange { | ||
24 | new_roots: Vec<(SourceRootId, bool)>, | ||
25 | roots_changed: FxHashMap<SourceRootId, RootChange>, | ||
26 | files_changed: Vec<(FileId, Arc<String>)>, | ||
27 | libraries_added: Vec<LibraryData>, | ||
28 | crate_graph: Option<CrateGraph>, | ||
29 | debug_data: DebugData, | ||
30 | } | ||
31 | |||
32 | impl fmt::Debug for AnalysisChange { | ||
33 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
34 | let mut d = fmt.debug_struct("AnalysisChange"); | ||
35 | if !self.new_roots.is_empty() { | ||
36 | d.field("new_roots", &self.new_roots); | ||
37 | } | ||
38 | if !self.roots_changed.is_empty() { | ||
39 | d.field("roots_changed", &self.roots_changed); | ||
40 | } | ||
41 | if !self.files_changed.is_empty() { | ||
42 | d.field("files_changed", &self.files_changed.len()); | ||
43 | } | ||
44 | if !self.libraries_added.is_empty() { | ||
45 | d.field("libraries_added", &self.libraries_added.len()); | ||
46 | } | ||
47 | if !self.crate_graph.is_none() { | ||
48 | d.field("crate_graph", &self.crate_graph); | ||
49 | } | ||
50 | d.finish() | ||
51 | } | ||
52 | } | ||
53 | |||
54 | impl AnalysisChange { | ||
55 | pub fn new() -> AnalysisChange { | ||
56 | AnalysisChange::default() | ||
57 | } | ||
58 | |||
59 | pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) { | ||
60 | self.new_roots.push((root_id, is_local)); | ||
61 | } | ||
62 | |||
63 | pub fn add_file( | ||
64 | &mut self, | ||
65 | root_id: SourceRootId, | ||
66 | file_id: FileId, | ||
67 | path: RelativePathBuf, | ||
68 | text: Arc<String>, | ||
69 | ) { | ||
70 | let file = AddFile { file_id, path, text }; | ||
71 | self.roots_changed.entry(root_id).or_default().added.push(file); | ||
72 | } | ||
73 | |||
74 | pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) { | ||
75 | self.files_changed.push((file_id, new_text)) | ||
76 | } | ||
77 | |||
78 | pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) { | ||
79 | let file = RemoveFile { file_id, path }; | ||
80 | self.roots_changed.entry(root_id).or_default().removed.push(file); | ||
81 | } | ||
82 | |||
83 | pub fn add_library(&mut self, data: LibraryData) { | ||
84 | self.libraries_added.push(data) | ||
85 | } | ||
86 | |||
87 | pub fn set_crate_graph(&mut self, graph: CrateGraph) { | ||
88 | self.crate_graph = Some(graph); | ||
89 | } | ||
90 | |||
91 | pub fn set_debug_crate_name(&mut self, crate_id: CrateId, name: String) { | ||
92 | self.debug_data.crate_names.insert(crate_id, name); | ||
93 | } | ||
94 | |||
95 | pub fn set_debug_root_path(&mut self, source_root_id: SourceRootId, path: String) { | ||
96 | self.debug_data.root_paths.insert(source_root_id, path); | ||
97 | } | ||
98 | } | ||
99 | |||
100 | #[derive(Debug)] | ||
101 | struct AddFile { | ||
102 | file_id: FileId, | ||
103 | path: RelativePathBuf, | ||
104 | text: Arc<String>, | ||
105 | } | ||
106 | |||
107 | #[derive(Debug)] | ||
108 | struct RemoveFile { | ||
109 | file_id: FileId, | ||
110 | path: RelativePathBuf, | ||
111 | } | ||
112 | |||
113 | #[derive(Default)] | ||
114 | struct RootChange { | ||
115 | added: Vec<AddFile>, | ||
116 | removed: Vec<RemoveFile>, | ||
117 | } | ||
118 | |||
119 | impl fmt::Debug for RootChange { | ||
120 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
121 | fmt.debug_struct("AnalysisChange") | ||
122 | .field("added", &self.added.len()) | ||
123 | .field("removed", &self.removed.len()) | ||
124 | .finish() | ||
125 | } | ||
126 | } | ||
127 | |||
128 | pub struct LibraryData { | ||
129 | root_id: SourceRootId, | ||
130 | root_change: RootChange, | ||
131 | symbol_index: SymbolIndex, | ||
132 | } | ||
133 | |||
134 | impl fmt::Debug for LibraryData { | ||
135 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
136 | f.debug_struct("LibraryData") | ||
137 | .field("root_id", &self.root_id) | ||
138 | .field("root_change", &self.root_change) | ||
139 | .field("n_symbols", &self.symbol_index.len()) | ||
140 | .finish() | ||
141 | } | ||
142 | } | ||
143 | |||
144 | impl LibraryData { | ||
145 | pub fn prepare( | ||
146 | root_id: SourceRootId, | ||
147 | files: Vec<(FileId, RelativePathBuf, Arc<String>)>, | ||
148 | ) -> LibraryData { | ||
149 | let _p = profile("LibraryData::prepare"); | ||
150 | |||
151 | #[cfg(not(feature = "wasm"))] | ||
152 | let iter = files.par_iter(); | ||
153 | #[cfg(feature = "wasm")] | ||
154 | let iter = files.iter(); | ||
155 | |||
156 | let symbol_index = SymbolIndex::for_files(iter.map(|(file_id, _, text)| { | ||
157 | let parse = SourceFile::parse(text); | ||
158 | (*file_id, parse) | ||
159 | })); | ||
160 | let mut root_change = RootChange::default(); | ||
161 | root_change.added = files | ||
162 | .into_iter() | ||
163 | .map(|(file_id, path, text)| AddFile { file_id, path, text }) | ||
164 | .collect(); | ||
165 | LibraryData { root_id, root_change, symbol_index } | ||
166 | } | ||
167 | } | ||
168 | |||
169 | const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100); | ||
170 | |||
171 | impl RootDatabase { | ||
172 | pub fn request_cancellation(&mut self) { | ||
173 | let _p = profile("RootDatabase::request_cancellation"); | ||
174 | self.salsa_runtime_mut().synthetic_write(Durability::LOW); | ||
175 | } | ||
176 | |||
177 | pub fn apply_change(&mut self, change: AnalysisChange) { | ||
178 | let _p = profile("RootDatabase::apply_change"); | ||
179 | self.request_cancellation(); | ||
180 | log::info!("apply_change {:?}", change); | ||
181 | if !change.new_roots.is_empty() { | ||
182 | let mut local_roots = Vec::clone(&self.local_roots()); | ||
183 | for (root_id, is_local) in change.new_roots { | ||
184 | let root = | ||
185 | if is_local { SourceRoot::new_local() } else { SourceRoot::new_library() }; | ||
186 | let durability = durability(&root); | ||
187 | self.set_source_root_with_durability(root_id, Arc::new(root), durability); | ||
188 | if is_local { | ||
189 | local_roots.push(root_id); | ||
190 | } | ||
191 | } | ||
192 | self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH); | ||
193 | } | ||
194 | |||
195 | for (root_id, root_change) in change.roots_changed { | ||
196 | self.apply_root_change(root_id, root_change); | ||
197 | } | ||
198 | for (file_id, text) in change.files_changed { | ||
199 | let source_root_id = self.file_source_root(file_id); | ||
200 | let source_root = self.source_root(source_root_id); | ||
201 | let durability = durability(&source_root); | ||
202 | self.set_file_text_with_durability(file_id, text, durability) | ||
203 | } | ||
204 | if !change.libraries_added.is_empty() { | ||
205 | let mut libraries = Vec::clone(&self.library_roots()); | ||
206 | for library in change.libraries_added { | ||
207 | libraries.push(library.root_id); | ||
208 | self.set_source_root_with_durability( | ||
209 | library.root_id, | ||
210 | Arc::new(SourceRoot::new_library()), | ||
211 | Durability::HIGH, | ||
212 | ); | ||
213 | self.set_library_symbols_with_durability( | ||
214 | library.root_id, | ||
215 | Arc::new(library.symbol_index), | ||
216 | Durability::HIGH, | ||
217 | ); | ||
218 | self.apply_root_change(library.root_id, library.root_change); | ||
219 | } | ||
220 | self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH); | ||
221 | } | ||
222 | if let Some(crate_graph) = change.crate_graph { | ||
223 | self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH) | ||
224 | } | ||
225 | |||
226 | Arc::make_mut(&mut self.debug_data).merge(change.debug_data) | ||
227 | } | ||
228 | |||
229 | fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) { | ||
230 | let mut source_root = SourceRoot::clone(&self.source_root(root_id)); | ||
231 | let durability = durability(&source_root); | ||
232 | for add_file in root_change.added { | ||
233 | self.set_file_text_with_durability(add_file.file_id, add_file.text, durability); | ||
234 | self.set_file_relative_path_with_durability( | ||
235 | add_file.file_id, | ||
236 | add_file.path.clone(), | ||
237 | durability, | ||
238 | ); | ||
239 | self.set_file_source_root_with_durability(add_file.file_id, root_id, durability); | ||
240 | source_root.insert_file(add_file.path, add_file.file_id); | ||
241 | } | ||
242 | for remove_file in root_change.removed { | ||
243 | self.set_file_text_with_durability(remove_file.file_id, Default::default(), durability); | ||
244 | source_root.remove_file(&remove_file.path); | ||
245 | } | ||
246 | self.set_source_root_with_durability(root_id, Arc::new(source_root), durability); | ||
247 | } | ||
248 | |||
249 | pub fn maybe_collect_garbage(&mut self) { | ||
250 | if cfg!(feature = "wasm") { | ||
251 | return; | ||
252 | } | ||
253 | |||
254 | if self.last_gc_check.elapsed() > GC_COOLDOWN { | ||
255 | self.last_gc_check = crate::wasm_shims::Instant::now(); | ||
256 | } | ||
257 | } | ||
258 | |||
259 | pub fn collect_garbage(&mut self) { | ||
260 | if cfg!(feature = "wasm") { | ||
261 | return; | ||
262 | } | ||
263 | |||
264 | let _p = profile("RootDatabase::collect_garbage"); | ||
265 | self.last_gc = crate::wasm_shims::Instant::now(); | ||
266 | |||
267 | let sweep = SweepStrategy::default().discard_values().sweep_all_revisions(); | ||
268 | |||
269 | self.query(ra_db::ParseQuery).sweep(sweep); | ||
270 | self.query(hir::db::ParseMacroQuery).sweep(sweep); | ||
271 | |||
272 | // Macros do take significant space, but less then the syntax trees | ||
273 | // self.query(hir::db::MacroDefQuery).sweep(sweep); | ||
274 | // self.query(hir::db::MacroArgQuery).sweep(sweep); | ||
275 | // self.query(hir::db::MacroExpandQuery).sweep(sweep); | ||
276 | |||
277 | self.query(hir::db::AstIdMapQuery).sweep(sweep); | ||
278 | |||
279 | self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep); | ||
280 | |||
281 | self.query(hir::db::ExprScopesQuery).sweep(sweep); | ||
282 | self.query(hir::db::DoInferQuery).sweep(sweep); | ||
283 | self.query(hir::db::BodyQuery).sweep(sweep); | ||
284 | } | ||
285 | |||
286 | pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> { | ||
287 | let mut acc: Vec<(String, Bytes)> = vec![]; | ||
288 | let sweep = SweepStrategy::default().discard_values().sweep_all_revisions(); | ||
289 | macro_rules! sweep_each_query { | ||
290 | ($($q:path)*) => {$( | ||
291 | let before = memory_usage().allocated; | ||
292 | self.query($q).sweep(sweep); | ||
293 | let after = memory_usage().allocated; | ||
294 | let q: $q = Default::default(); | ||
295 | let name = format!("{:?}", q); | ||
296 | acc.push((name, before - after)); | ||
297 | |||
298 | let before = memory_usage().allocated; | ||
299 | self.query($q).sweep(sweep.discard_everything()); | ||
300 | let after = memory_usage().allocated; | ||
301 | let q: $q = Default::default(); | ||
302 | let name = format!("{:?} (deps)", q); | ||
303 | acc.push((name, before - after)); | ||
304 | )*} | ||
305 | } | ||
306 | sweep_each_query![ | ||
307 | // SourceDatabase | ||
308 | ra_db::ParseQuery | ||
309 | ra_db::SourceRootCratesQuery | ||
310 | |||
311 | // AstDatabase | ||
312 | hir::db::AstIdMapQuery | ||
313 | hir::db::InternMacroQuery | ||
314 | hir::db::MacroArgQuery | ||
315 | hir::db::MacroDefQuery | ||
316 | hir::db::ParseMacroQuery | ||
317 | hir::db::MacroExpandQuery | ||
318 | |||
319 | // DefDatabase | ||
320 | hir::db::RawItemsQuery | ||
321 | hir::db::ComputeCrateDefMapQuery | ||
322 | hir::db::StructDataQuery | ||
323 | hir::db::UnionDataQuery | ||
324 | hir::db::EnumDataQuery | ||
325 | hir::db::ImplDataQuery | ||
326 | hir::db::TraitDataQuery | ||
327 | hir::db::TypeAliasDataQuery | ||
328 | hir::db::FunctionDataQuery | ||
329 | hir::db::ConstDataQuery | ||
330 | hir::db::StaticDataQuery | ||
331 | hir::db::BodyWithSourceMapQuery | ||
332 | hir::db::BodyQuery | ||
333 | hir::db::ExprScopesQuery | ||
334 | hir::db::GenericParamsQuery | ||
335 | hir::db::AttrsQuery | ||
336 | hir::db::ModuleLangItemsQuery | ||
337 | hir::db::CrateLangItemsQuery | ||
338 | hir::db::LangItemQuery | ||
339 | hir::db::DocumentationQuery | ||
340 | |||
341 | // InternDatabase | ||
342 | hir::db::InternFunctionQuery | ||
343 | hir::db::InternStructQuery | ||
344 | hir::db::InternUnionQuery | ||
345 | hir::db::InternEnumQuery | ||
346 | hir::db::InternConstQuery | ||
347 | hir::db::InternStaticQuery | ||
348 | hir::db::InternTraitQuery | ||
349 | hir::db::InternTypeAliasQuery | ||
350 | hir::db::InternImplQuery | ||
351 | |||
352 | // HirDatabase | ||
353 | hir::db::DoInferQuery | ||
354 | hir::db::TyQuery | ||
355 | hir::db::ValueTyQuery | ||
356 | hir::db::ImplSelfTyQuery | ||
357 | hir::db::ImplTraitQuery | ||
358 | hir::db::FieldTypesQuery | ||
359 | hir::db::CallableItemSignatureQuery | ||
360 | hir::db::GenericPredicatesForParamQuery | ||
361 | hir::db::GenericPredicatesQuery | ||
362 | hir::db::GenericDefaultsQuery | ||
363 | hir::db::ImplsInCrateQuery | ||
364 | hir::db::ImplsForTraitQuery | ||
365 | hir::db::TraitSolverQuery | ||
366 | hir::db::InternTypeCtorQuery | ||
367 | hir::db::InternChalkImplQuery | ||
368 | hir::db::InternAssocTyValueQuery | ||
369 | hir::db::AssociatedTyDataQuery | ||
370 | hir::db::AssociatedTyValueQuery | ||
371 | hir::db::TraitSolveQuery | ||
372 | hir::db::TraitDatumQuery | ||
373 | hir::db::StructDatumQuery | ||
374 | hir::db::ImplDatumQuery | ||
375 | ]; | ||
376 | acc.sort_by_key(|it| std::cmp::Reverse(it.1)); | ||
377 | acc | ||
378 | } | ||
379 | } | ||
380 | |||
381 | fn durability(source_root: &SourceRoot) -> Durability { | ||
382 | if source_root.is_library { | ||
383 | Durability::HIGH | ||
384 | } else { | ||
385 | Durability::LOW | ||
386 | } | ||
387 | } | ||