1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::sync::Arc;
use base_db::{
salsa::{Database, Durability, SweepStrategy},
Change, SourceRootId,
};
use profile::{memory_usage, Bytes};
use rustc_hash::FxHashSet;
use crate::{symbol_index::SymbolsDatabase, RootDatabase};
impl RootDatabase {
pub fn request_cancellation(&mut self) {
let _p = profile::span("RootDatabase::request_cancellation");
self.salsa_runtime_mut().synthetic_write(Durability::LOW);
}
pub fn apply_change(&mut self, change: Change) {
let _p = profile::span("RootDatabase::apply_change");
self.request_cancellation();
tracing::info!("apply_change {:?}", change);
if let Some(roots) = &change.roots {
let mut local_roots = FxHashSet::default();
let mut library_roots = FxHashSet::default();
for (idx, root) in roots.iter().enumerate() {
let root_id = SourceRootId(idx as u32);
if root.is_library {
library_roots.insert(root_id);
} else {
local_roots.insert(root_id);
}
}
self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
self.set_library_roots_with_durability(Arc::new(library_roots), Durability::HIGH);
}
change.apply(self);
}
pub fn collect_garbage(&mut self) {
if cfg!(target_arch = "wasm32") {
return;
}
let _p = profile::span("RootDatabase::collect_garbage");
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
base_db::ParseQuery.in_db(self).sweep(sweep);
hir::db::ParseMacroExpansionQuery.in_db(self).sweep(sweep);
hir::db::AstIdMapQuery.in_db(self).sweep(sweep);
hir::db::BodyWithSourceMapQuery.in_db(self).sweep(sweep);
hir::db::ExprScopesQuery.in_db(self).sweep(sweep);
hir::db::InferQueryQuery.in_db(self).sweep(sweep);
hir::db::BodyQuery.in_db(self).sweep(sweep);
}
pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> {
let mut acc: Vec<(String, Bytes)> = vec![];
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
macro_rules! sweep_each_query {
($($q:path)*) => {$(
let before = memory_usage().allocated;
$q.in_db(self).sweep(sweep);
let after = memory_usage().allocated;
let q: $q = Default::default();
let name = format!("{:?}", q);
acc.push((name, before - after));
let before = memory_usage().allocated;
$q.in_db(self).sweep(sweep.discard_everything());
let after = memory_usage().allocated;
let q: $q = Default::default();
let name = format!("{:?} (deps)", q);
acc.push((name, before - after));
let before = memory_usage().allocated;
$q.in_db(self).purge();
let after = memory_usage().allocated;
let q: $q = Default::default();
let name = format!("{:?} (purge)", q);
acc.push((name, before - after));
)*}
}
sweep_each_query![
base_db::ParseQuery
base_db::CrateGraphQuery
base_db::FileTextQuery
base_db::FileSourceRootQuery
base_db::SourceRootQuery
base_db::SourceRootCratesQuery
hir::db::AstIdMapQuery
hir::db::MacroArgTextQuery
hir::db::MacroDefQuery
hir::db::ParseMacroExpansionQuery
hir::db::MacroExpandQuery
hir::db::HygieneFrameQuery
hir::db::FileItemTreeQuery
hir::db::BlockDefMapQuery
hir::db::CrateDefMapQueryQuery
hir::db::FieldsAttrsQuery
hir::db::VariantsAttrsQuery
hir::db::FieldsAttrsSourceMapQuery
hir::db::VariantsAttrsSourceMapQuery
hir::db::StructDataQuery
hir::db::UnionDataQuery
hir::db::EnumDataQuery
hir::db::ImplDataQuery
hir::db::TraitDataQuery
hir::db::TypeAliasDataQuery
hir::db::FunctionDataQuery
hir::db::ConstDataQuery
hir::db::StaticDataQuery
hir::db::BodyWithSourceMapQuery
hir::db::BodyQuery
hir::db::ExprScopesQuery
hir::db::GenericParamsQuery
hir::db::AttrsQuery
hir::db::CrateLangItemsQuery
hir::db::LangItemQuery
hir::db::ImportMapQuery
hir::db::InferQueryQuery
hir::db::TyQuery
hir::db::ValueTyQuery
hir::db::ImplSelfTyQuery
hir::db::ImplTraitQuery
hir::db::FieldTypesQuery
hir::db::CallableItemSignatureQuery
hir::db::GenericPredicatesForParamQuery
hir::db::GenericPredicatesQuery
hir::db::GenericDefaultsQuery
hir::db::InherentImplsInCrateQuery
hir::db::TraitEnvironmentQuery
hir::db::TraitImplsInCrateQuery
hir::db::TraitImplsInDepsQuery
hir::db::AssociatedTyDataQuery
hir::db::AssociatedTyDataQuery
hir::db::TraitDatumQuery
hir::db::StructDatumQuery
hir::db::ImplDatumQuery
hir::db::FnDefDatumQuery
hir::db::ReturnTypeImplTraitsQuery
hir::db::InternCallableDefQuery
hir::db::InternTypeParamIdQuery
hir::db::InternImplTraitIdQuery
hir::db::InternClosureQuery
hir::db::AssociatedTyValueQuery
hir::db::TraitSolveQueryQuery
crate::symbol_index::FileSymbolsQuery
crate::symbol_index::LibrarySymbolsQuery
crate::symbol_index::LocalRootsQuery
crate::symbol_index::LibraryRootsQuery
crate::LineIndexQuery
];
self.salsa_runtime_mut().synthetic_write(Durability::HIGH);
sweep_each_query![
hir::db::InternMacroQuery
hir::db::InternFunctionQuery
hir::db::InternStructQuery
hir::db::InternUnionQuery
hir::db::InternEnumQuery
hir::db::InternConstQuery
hir::db::InternStaticQuery
hir::db::InternTraitQuery
hir::db::InternTypeAliasQuery
hir::db::InternImplQuery
hir::db::InternTypeParamIdQuery
];
acc.sort_by_key(|it| std::cmp::Reverse(it.1));
acc
}
}