aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/change.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/change.rs')
-rw-r--r--crates/ra_ide/src/change.rs353
1 files changed, 0 insertions, 353 deletions
diff --git a/crates/ra_ide/src/change.rs b/crates/ra_ide/src/change.rs
deleted file mode 100644
index b0aa2c8e0..000000000
--- a/crates/ra_ide/src/change.rs
+++ /dev/null
@@ -1,353 +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::{
17 db::{DebugData, RootDatabase},
18 symbol_index::{SymbolIndex, SymbolsDatabase},
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 #[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
166const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100);
167
168impl 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 =
180 if is_local { SourceRoot::new_local() } else { SourceRoot::new_library() };
181 let durability = durability(&root);
182 self.set_source_root_with_durability(root_id, Arc::new(root), durability);
183 if is_local {
184 local_roots.push(root_id);
185 }
186 }
187 self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
188 }
189
190 for (root_id, root_change) in change.roots_changed {
191 self.apply_root_change(root_id, root_change);
192 }
193 for (file_id, text) in change.files_changed {
194 let source_root_id = self.file_source_root(file_id);
195 let source_root = self.source_root(source_root_id);
196 let durability = durability(&source_root);
197 self.set_file_text_with_durability(file_id, text, durability)
198 }
199 if !change.libraries_added.is_empty() {
200 let mut libraries = Vec::clone(&self.library_roots());
201 for library in change.libraries_added {
202 libraries.push(library.root_id);
203 self.set_source_root_with_durability(
204 library.root_id,
205 Arc::new(SourceRoot::new_library()),
206 Durability::HIGH,
207 );
208 self.set_library_symbols_with_durability(
209 library.root_id,
210 Arc::new(library.symbol_index),
211 Durability::HIGH,
212 );
213 self.apply_root_change(library.root_id, library.root_change);
214 }
215 self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH);
216 }
217 if let Some(crate_graph) = change.crate_graph {
218 self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH)
219 }
220
221 Arc::make_mut(&mut self.debug_data).merge(change.debug_data)
222 }
223
224 fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) {
225 let mut source_root = SourceRoot::clone(&self.source_root(root_id));
226 let durability = durability(&source_root);
227 for add_file in root_change.added {
228 self.set_file_text_with_durability(add_file.file_id, add_file.text, durability);
229 self.set_file_relative_path_with_durability(
230 add_file.file_id,
231 add_file.path.clone(),
232 durability,
233 );
234 self.set_file_source_root_with_durability(add_file.file_id, root_id, durability);
235 source_root.insert_file(add_file.path, add_file.file_id);
236 }
237 for remove_file in root_change.removed {
238 self.set_file_text_with_durability(remove_file.file_id, Default::default(), durability);
239 source_root.remove_file(&remove_file.path);
240 }
241 self.set_source_root_with_durability(root_id, Arc::new(source_root), durability);
242 }
243
244 pub(crate) fn maybe_collect_garbage(&mut self) {
245 if cfg!(feature = "wasm") {
246 return;
247 }
248
249 if self.last_gc_check.elapsed() > GC_COOLDOWN {
250 self.last_gc_check = crate::wasm_shims::Instant::now();
251 }
252 }
253
254 pub(crate) fn collect_garbage(&mut self) {
255 if cfg!(feature = "wasm") {
256 return;
257 }
258
259 let _p = profile("RootDatabase::collect_garbage");
260 self.last_gc = crate::wasm_shims::Instant::now();
261
262 let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
263
264 self.query(ra_db::ParseQuery).sweep(sweep);
265 self.query(hir::db::ParseMacroQuery).sweep(sweep);
266
267 // Macros do take significant space, but less then the syntax trees
268 // self.query(hir::db::MacroDefQuery).sweep(sweep);
269 // self.query(hir::db::MacroArgQuery).sweep(sweep);
270 // self.query(hir::db::MacroExpandQuery).sweep(sweep);
271
272 self.query(hir::db::AstIdMapQuery).sweep(sweep);
273
274 self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep);
275
276 self.query(hir::db::ExprScopesQuery).sweep(sweep);
277 self.query(hir::db::DoInferQuery).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::RawItemsQuery
313 hir::db::ComputeCrateDefMapQuery
314 hir::db::GenericParamsQuery
315 hir::db::FunctionDataQuery
316 hir::db::TypeAliasDataQuery
317 hir::db::ConstDataQuery
318 hir::db::StaticDataQuery
319 hir::db::ModuleLangItemsQuery
320 hir::db::CrateLangItemsQuery
321 hir::db::LangItemQuery
322 hir::db::DocumentationQuery
323 hir::db::ExprScopesQuery
324 hir::db::DoInferQuery
325 hir::db::TyQuery
326 hir::db::ValueTyQuery
327 hir::db::FieldTypesQuery
328 hir::db::CallableItemSignatureQuery
329 hir::db::GenericPredicatesQuery
330 hir::db::GenericDefaultsQuery
331 hir::db::BodyWithSourceMapQuery
332 hir::db::BodyQuery
333 hir::db::ImplsInCrateQuery
334 hir::db::ImplsForTraitQuery
335 hir::db::AssociatedTyDataQuery
336 hir::db::TraitDatumQuery
337 hir::db::StructDatumQuery
338 hir::db::ImplDatumQuery
339 hir::db::ImplDataQuery
340 hir::db::TraitSolveQuery
341 ];
342 acc.sort_by_key(|it| std::cmp::Reverse(it.1));
343 acc
344 }
345}
346
347fn durability(source_root: &SourceRoot) -> Durability {
348 if source_root.is_library {
349 Durability::HIGH
350 } else {
351 Durability::LOW
352 }
353}