diff options
author | Seivan Heidari <[email protected]> | 2019-11-28 07:19:14 +0000 |
---|---|---|
committer | Seivan Heidari <[email protected]> | 2019-11-28 07:19:14 +0000 |
commit | 18a0937585b836ec5ed054b9ae48e0156ab6d9ef (patch) | |
tree | 9de2c0267ddcc00df717f90034d0843d751a851b /crates/ra_ide/src/change.rs | |
parent | a7394b44c870f585eacfeb3036a33471aff49ff8 (diff) | |
parent | 484acc8a61d599662ed63a4cbda091d38a982551 (diff) |
Merge branch 'master' of https://github.com/rust-analyzer/rust-analyzer into feature/themes
Diffstat (limited to 'crates/ra_ide/src/change.rs')
-rw-r--r-- | crates/ra_ide/src/change.rs | 354 |
1 files changed, 354 insertions, 0 deletions
diff --git a/crates/ra_ide/src/change.rs b/crates/ra_ide/src/change.rs new file mode 100644 index 000000000..4a76d1dd8 --- /dev/null +++ b/crates/ra_ide/src/change.rs | |||
@@ -0,0 +1,354 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use std::{fmt, sync::Arc, time}; | ||
4 | |||
5 | use ra_db::{ | ||
6 | salsa::{Database, Durability, SweepStrategy}, | ||
7 | CrateGraph, CrateId, FileId, RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot, | ||
8 | SourceRootId, | ||
9 | }; | ||
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; | ||
15 | |||
16 | use crate::{ | ||
17 | db::{DebugData, RootDatabase}, | ||
18 | symbol_index::{SymbolIndex, SymbolsDatabase}, | ||
19 | }; | ||
20 | |||
21 | #[derive(Default)] | ||
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, | ||
29 | } | ||
30 | |||
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); | ||
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 | |||
53 | impl 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)] | ||
100 | struct AddFile { | ||
101 | file_id: FileId, | ||
102 | path: RelativePathBuf, | ||
103 | text: Arc<String>, | ||
104 | } | ||
105 | |||
106 | #[derive(Debug)] | ||
107 | struct RemoveFile { | ||
108 | file_id: FileId, | ||
109 | path: RelativePathBuf, | ||
110 | } | ||
111 | |||
112 | #[derive(Default)] | ||
113 | struct RootChange { | ||
114 | added: Vec<AddFile>, | ||
115 | removed: Vec<RemoveFile>, | ||
116 | } | ||
117 | |||
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()) | ||
123 | .finish() | ||
124 | } | ||
125 | } | ||
126 | |||
127 | pub struct LibraryData { | ||
128 | root_id: SourceRootId, | ||
129 | root_change: RootChange, | ||
130 | symbol_index: SymbolIndex, | ||
131 | } | ||
132 | |||
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()) | ||
139 | .finish() | ||
140 | } | ||
141 | } | ||
142 | |||
143 | impl LibraryData { | ||
144 | pub fn prepare( | ||
145 | root_id: SourceRootId, | ||
146 | files: Vec<(FileId, RelativePathBuf, Arc<String>)>, | ||
147 | ) -> LibraryData { | ||
148 | #[cfg(not(feature = "wasm"))] | ||
149 | let iter = files.par_iter(); | ||
150 | #[cfg(feature = "wasm")] | ||
151 | let iter = files.iter(); | ||
152 | |||
153 | let symbol_index = SymbolIndex::for_files(iter.map(|(file_id, _, text)| { | ||
154 | let parse = SourceFile::parse(text); | ||
155 | (*file_id, parse) | ||
156 | })); | ||
157 | let mut root_change = RootChange::default(); | ||
158 | root_change.added = files | ||
159 | .into_iter() | ||
160 | .map(|(file_id, path, text)| AddFile { file_id, path, text }) | ||
161 | .collect(); | ||
162 | LibraryData { root_id, root_change, symbol_index } | ||
163 | } | ||
164 | } | ||
165 | |||
166 | const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100); | ||
167 | |||
168 | impl RootDatabase { | ||
169 | pub(crate) fn apply_change(&mut self, change: AnalysisChange) { | ||
170 | let _p = profile("RootDatabase::apply_change"); | ||
171 | log::info!("apply_change {:?}", change); | ||
172 | { | ||
173 | let _p = profile("RootDatabase::apply_change/cancellation"); | ||
174 | self.salsa_runtime_mut().synthetic_write(Durability::LOW); | ||
175 | } | ||
176 | if !change.new_roots.is_empty() { | ||
177 | let mut local_roots = Vec::clone(&self.local_roots()); | ||
178 | for (root_id, is_local) in change.new_roots { | ||
179 | let root = if is_local { SourceRoot::new() } else { SourceRoot::new_library() }; | ||
180 | let durability = durability(&root); | ||
181 | self.set_source_root_with_durability(root_id, Arc::new(root), durability); | ||
182 | if is_local { | ||
183 | local_roots.push(root_id); | ||
184 | } | ||
185 | } | ||
186 | self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH); | ||
187 | } | ||
188 | |||
189 | for (root_id, root_change) in change.roots_changed { | ||
190 | self.apply_root_change(root_id, root_change); | ||
191 | } | ||
192 | for (file_id, text) in change.files_changed { | ||
193 | let source_root_id = self.file_source_root(file_id); | ||
194 | let source_root = self.source_root(source_root_id); | ||
195 | let durability = durability(&source_root); | ||
196 | self.set_file_text_with_durability(file_id, text, durability) | ||
197 | } | ||
198 | if !change.libraries_added.is_empty() { | ||
199 | let mut libraries = Vec::clone(&self.library_roots()); | ||
200 | for library in change.libraries_added { | ||
201 | libraries.push(library.root_id); | ||
202 | self.set_source_root_with_durability( | ||
203 | library.root_id, | ||
204 | Default::default(), | ||
205 | Durability::HIGH, | ||
206 | ); | ||
207 | self.set_library_symbols_with_durability( | ||
208 | library.root_id, | ||
209 | Arc::new(library.symbol_index), | ||
210 | Durability::HIGH, | ||
211 | ); | ||
212 | self.apply_root_change(library.root_id, library.root_change); | ||
213 | } | ||
214 | self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH); | ||
215 | } | ||
216 | if let Some(crate_graph) = change.crate_graph { | ||
217 | self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH) | ||
218 | } | ||
219 | |||
220 | Arc::make_mut(&mut self.debug_data).merge(change.debug_data) | ||
221 | } | ||
222 | |||
223 | fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) { | ||
224 | let mut source_root = SourceRoot::clone(&self.source_root(root_id)); | ||
225 | let durability = durability(&source_root); | ||
226 | for add_file in root_change.added { | ||
227 | self.set_file_text_with_durability(add_file.file_id, add_file.text, durability); | ||
228 | self.set_file_relative_path_with_durability( | ||
229 | add_file.file_id, | ||
230 | add_file.path.clone(), | ||
231 | durability, | ||
232 | ); | ||
233 | self.set_file_source_root_with_durability(add_file.file_id, root_id, durability); | ||
234 | source_root.insert_file(add_file.path, add_file.file_id); | ||
235 | } | ||
236 | for remove_file in root_change.removed { | ||
237 | self.set_file_text_with_durability(remove_file.file_id, Default::default(), durability); | ||
238 | source_root.remove_file(&remove_file.path); | ||
239 | } | ||
240 | self.set_source_root_with_durability(root_id, Arc::new(source_root), durability); | ||
241 | } | ||
242 | |||
243 | pub(crate) fn maybe_collect_garbage(&mut self) { | ||
244 | if cfg!(feature = "wasm") { | ||
245 | return; | ||
246 | } | ||
247 | |||
248 | if self.last_gc_check.elapsed() > GC_COOLDOWN { | ||
249 | self.last_gc_check = crate::wasm_shims::Instant::now(); | ||
250 | } | ||
251 | } | ||
252 | |||
253 | pub(crate) fn collect_garbage(&mut self) { | ||
254 | if cfg!(feature = "wasm") { | ||
255 | return; | ||
256 | } | ||
257 | |||
258 | let _p = profile("RootDatabase::collect_garbage"); | ||
259 | self.last_gc = crate::wasm_shims::Instant::now(); | ||
260 | |||
261 | let sweep = SweepStrategy::default().discard_values().sweep_all_revisions(); | ||
262 | |||
263 | self.query(ra_db::ParseQuery).sweep(sweep); | ||
264 | self.query(hir::db::ParseMacroQuery).sweep(sweep); | ||
265 | |||
266 | // Macros do take significant space, but less then the syntax trees | ||
267 | // self.query(hir::db::MacroDefQuery).sweep(sweep); | ||
268 | // self.query(hir::db::MacroArgQuery).sweep(sweep); | ||
269 | // self.query(hir::db::MacroExpandQuery).sweep(sweep); | ||
270 | |||
271 | self.query(hir::db::AstIdMapQuery).sweep(sweep); | ||
272 | |||
273 | self.query(hir::db::RawItemsWithSourceMapQuery).sweep(sweep); | ||
274 | self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep); | ||
275 | |||
276 | self.query(hir::db::ExprScopesQuery).sweep(sweep); | ||
277 | self.query(hir::db::InferQuery).sweep(sweep); | ||
278 | self.query(hir::db::BodyQuery).sweep(sweep); | ||
279 | } | ||
280 | |||
281 | pub(crate) fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> { | ||
282 | let mut acc: Vec<(String, Bytes)> = vec![]; | ||
283 | let sweep = SweepStrategy::default().discard_values().sweep_all_revisions(); | ||
284 | macro_rules! sweep_each_query { | ||
285 | ($($q:path)*) => {$( | ||
286 | let before = memory_usage().allocated; | ||
287 | self.query($q).sweep(sweep); | ||
288 | let after = memory_usage().allocated; | ||
289 | let q: $q = Default::default(); | ||
290 | let name = format!("{:?}", q); | ||
291 | acc.push((name, before - after)); | ||
292 | |||
293 | let before = memory_usage().allocated; | ||
294 | self.query($q).sweep(sweep.discard_everything()); | ||
295 | let after = memory_usage().allocated; | ||
296 | let q: $q = Default::default(); | ||
297 | let name = format!("{:?} (deps)", q); | ||
298 | acc.push((name, before - after)); | ||
299 | )*} | ||
300 | } | ||
301 | sweep_each_query![ | ||
302 | ra_db::ParseQuery | ||
303 | ra_db::SourceRootCratesQuery | ||
304 | hir::db::AstIdMapQuery | ||
305 | hir::db::ParseMacroQuery | ||
306 | hir::db::MacroDefQuery | ||
307 | hir::db::MacroArgQuery | ||
308 | hir::db::MacroExpandQuery | ||
309 | hir::db::StructDataQuery | ||
310 | hir::db::EnumDataQuery | ||
311 | hir::db::TraitDataQuery | ||
312 | hir::db::RawItemsWithSourceMapQuery | ||
313 | hir::db::RawItemsQuery | ||
314 | hir::db::CrateDefMapQuery | ||
315 | hir::db::GenericParamsQuery | ||
316 | hir::db::FunctionDataQuery | ||
317 | hir::db::TypeAliasDataQuery | ||
318 | hir::db::ConstDataQuery | ||
319 | hir::db::StaticDataQuery | ||
320 | hir::db::ModuleLangItemsQuery | ||
321 | hir::db::CrateLangItemsQuery | ||
322 | hir::db::LangItemQuery | ||
323 | hir::db::DocumentationQuery | ||
324 | hir::db::ExprScopesQuery | ||
325 | hir::db::InferQuery | ||
326 | hir::db::TyQuery | ||
327 | hir::db::ValueTyQuery | ||
328 | hir::db::FieldTypesQuery | ||
329 | hir::db::CallableItemSignatureQuery | ||
330 | hir::db::GenericPredicatesQuery | ||
331 | hir::db::GenericDefaultsQuery | ||
332 | hir::db::BodyWithSourceMapQuery | ||
333 | hir::db::BodyQuery | ||
334 | hir::db::ImplsInCrateQuery | ||
335 | hir::db::ImplsForTraitQuery | ||
336 | hir::db::AssociatedTyDataQuery | ||
337 | hir::db::TraitDatumQuery | ||
338 | hir::db::StructDatumQuery | ||
339 | hir::db::ImplDatumQuery | ||
340 | hir::db::ImplDataQuery | ||
341 | hir::db::TraitSolveQuery | ||
342 | ]; | ||
343 | acc.sort_by_key(|it| std::cmp::Reverse(it.1)); | ||
344 | acc | ||
345 | } | ||
346 | } | ||
347 | |||
348 | fn durability(source_root: &SourceRoot) -> Durability { | ||
349 | if source_root.is_library { | ||
350 | Durability::HIGH | ||
351 | } else { | ||
352 | Durability::LOW | ||
353 | } | ||
354 | } | ||