Skip to content

Commit 24e0d51

Browse files
committed
Make LRU opt-in
1 parent f1741aa commit 24e0d51

File tree

12 files changed

+1159
-342
lines changed

12 files changed

+1159
-342
lines changed

src/tools/rust-analyzer/crates/base-db/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub trait FileLoader {
5959
#[salsa::query_group(SourceDatabaseStorage)]
6060
pub trait SourceDatabase: FileLoader + std::fmt::Debug {
6161
/// Parses the file into the syntax tree.
62+
#[salsa::lru]
6263
fn parse(&self, file_id: EditionedFileId) -> Parse<ast::SourceFile>;
6364

6465
/// Returns the set of errors obtained from parsing the file including validation errors.
@@ -105,6 +106,7 @@ pub trait SourceDatabaseExt: SourceDatabase {
105106
#[salsa::input]
106107
fn compressed_file_text(&self, file_id: FileId) -> Arc<[u8]>;
107108

109+
#[salsa::lru]
108110
fn file_text(&self, file_id: FileId) -> Arc<str>;
109111

110112
/// Path to a file, relative to the root of its source root.

src/tools/rust-analyzer/crates/hir-expand/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub trait ExpandDatabase: SourceDatabase {
6565
#[salsa::transparent]
6666
fn parse_or_expand_with_err(&self, file_id: HirFileId) -> ExpandResult<Parse<SyntaxNode>>;
6767
/// Implementation for the macro case.
68-
// This query is LRU cached
68+
#[salsa::lru]
6969
fn parse_macro_expansion(
7070
&self,
7171
macro_file: MacroFileId,

src/tools/rust-analyzer/crates/hir-ty/src/db.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
6161
) -> Result<Arc<MirBody>, MirLowerError>;
6262

6363
#[salsa::invoke(crate::mir::borrowck_query)]
64+
#[salsa::lru]
6465
fn borrowck(&self, def: DefWithBodyId) -> Result<Arc<[BorrowckResult]>, MirLowerError>;
6566

6667
#[salsa::invoke(crate::consteval::const_eval_query)]

src/tools/rust-analyzer/crates/ide-db/src/lib.rs

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -192,135 +192,6 @@ impl RootDatabase {
192192
.copied()
193193
.unwrap_or(base_db::DEFAULT_BORROWCK_LRU_CAP),
194194
);
195-
196-
macro_rules! update_lru_capacity_per_query {
197-
($( $module:ident :: $query:ident )*) => {$(
198-
if let Some(&cap) = lru_capacities.get(stringify!($query)) {
199-
$module::$query.in_db_mut(self).set_lru_capacity(cap);
200-
}
201-
)*}
202-
}
203-
update_lru_capacity_per_query![
204-
// SourceDatabase
205-
// base_db::ParseQuery
206-
// base_db::CrateGraphQuery
207-
// base_db::ProcMacrosQuery
208-
209-
// SourceDatabaseExt
210-
base_db::FileTextQuery
211-
// base_db::FileSourceRootQuery
212-
// base_db::SourceRootQuery
213-
base_db::SourceRootCratesQuery
214-
215-
// ExpandDatabase
216-
hir_db::AstIdMapQuery
217-
// hir_db::ParseMacroExpansionQuery
218-
// hir_db::InternMacroCallQuery
219-
hir_db::MacroArgQuery
220-
hir_db::DeclMacroExpanderQuery
221-
// hir_db::MacroExpandQuery
222-
hir_db::ExpandProcMacroQuery
223-
hir_db::ParseMacroExpansionErrorQuery
224-
225-
// DefDatabase
226-
hir_db::FileItemTreeQuery
227-
hir_db::BlockDefMapQuery
228-
hir_db::StructDataWithDiagnosticsQuery
229-
hir_db::UnionDataWithDiagnosticsQuery
230-
hir_db::EnumDataQuery
231-
hir_db::EnumVariantDataWithDiagnosticsQuery
232-
hir_db::ImplDataWithDiagnosticsQuery
233-
hir_db::TraitDataWithDiagnosticsQuery
234-
hir_db::TraitAliasDataQuery
235-
hir_db::TypeAliasDataQuery
236-
hir_db::FunctionDataQuery
237-
hir_db::ConstDataQuery
238-
hir_db::StaticDataQuery
239-
hir_db::Macro2DataQuery
240-
hir_db::MacroRulesDataQuery
241-
hir_db::ProcMacroDataQuery
242-
hir_db::BodyWithSourceMapQuery
243-
hir_db::BodyQuery
244-
hir_db::ExprScopesQuery
245-
hir_db::GenericParamsQuery
246-
hir_db::FieldsAttrsQuery
247-
hir_db::FieldsAttrsSourceMapQuery
248-
hir_db::AttrsQuery
249-
hir_db::CrateLangItemsQuery
250-
hir_db::LangItemQuery
251-
hir_db::ImportMapQuery
252-
hir_db::FieldVisibilitiesQuery
253-
hir_db::FunctionVisibilityQuery
254-
hir_db::ConstVisibilityQuery
255-
hir_db::CrateSupportsNoStdQuery
256-
257-
// HirDatabase
258-
hir_db::MirBodyQuery
259-
hir_db::BorrowckQuery
260-
hir_db::TyQuery
261-
hir_db::ValueTyQuery
262-
hir_db::ImplSelfTyQuery
263-
hir_db::ConstParamTyQuery
264-
hir_db::ConstEvalQuery
265-
hir_db::ConstEvalDiscriminantQuery
266-
hir_db::ImplTraitQuery
267-
hir_db::FieldTypesQuery
268-
hir_db::LayoutOfAdtQuery
269-
hir_db::TargetDataLayoutQuery
270-
hir_db::CallableItemSignatureQuery
271-
hir_db::ReturnTypeImplTraitsQuery
272-
hir_db::GenericPredicatesForParamQuery
273-
hir_db::GenericPredicatesQuery
274-
hir_db::TraitEnvironmentQuery
275-
hir_db::GenericDefaultsQuery
276-
hir_db::InherentImplsInCrateQuery
277-
hir_db::InherentImplsInBlockQuery
278-
hir_db::IncoherentInherentImplCratesQuery
279-
hir_db::TraitImplsInCrateQuery
280-
hir_db::TraitImplsInBlockQuery
281-
hir_db::TraitImplsInDepsQuery
282-
// hir_db::InternCallableDefQuery
283-
// hir_db::InternLifetimeParamIdQuery
284-
// hir_db::InternImplTraitIdQuery
285-
// hir_db::InternTypeOrConstParamIdQuery
286-
// hir_db::InternClosureQuery
287-
// hir_db::InternCoroutineQuery
288-
hir_db::AssociatedTyDataQuery
289-
hir_db::TraitDatumQuery
290-
hir_db::AdtDatumQuery
291-
hir_db::ImplDatumQuery
292-
hir_db::FnDefDatumQuery
293-
hir_db::FnDefVarianceQuery
294-
hir_db::AdtVarianceQuery
295-
hir_db::AssociatedTyValueQuery
296-
hir_db::ProgramClausesForChalkEnvQuery
297-
298-
// SymbolsDatabase
299-
symbol_index::ModuleSymbolsQuery
300-
symbol_index::LibrarySymbolsQuery
301-
// symbol_index::LocalRootsQuery
302-
// symbol_index::LibraryRootsQuery
303-
304-
// LineIndexDatabase
305-
crate::LineIndexQuery
306-
307-
// InternDatabase
308-
// hir_db::InternFunctionQuery
309-
// hir_db::InternStructQuery
310-
// hir_db::InternUnionQuery
311-
// hir_db::InternEnumQuery
312-
// hir_db::InternConstQuery
313-
// hir_db::InternStaticQuery
314-
// hir_db::InternTraitQuery
315-
// hir_db::InternTraitAliasQuery
316-
// hir_db::InternTypeAliasQuery
317-
// hir_db::InternImplQuery
318-
// hir_db::InternExternBlockQuery
319-
// hir_db::InternBlockQuery
320-
// hir_db::InternMacro2Query
321-
// hir_db::InternProcMacroQuery
322-
// hir_db::InternMacroRulesQuery
323-
];
324195
}
325196
}
326197

src/tools/rust-analyzer/crates/salsa/salsa-macros/src/query_group.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
5353
num_storages += 1;
5454
}
5555
"dependencies" => {
56-
storage = QueryStorage::Dependencies;
56+
storage = QueryStorage::LruDependencies;
57+
num_storages += 1;
58+
}
59+
"lru" => {
60+
storage = QueryStorage::LruMemoized;
5761
num_storages += 1;
5862
}
5963
"input" => {
@@ -235,7 +239,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
235239

236240
queries_with_storage.push(fn_name);
237241

238-
let tracing = if let QueryStorage::Memoized = query.storage {
242+
let tracing = if let QueryStorage::Memoized | QueryStorage::LruMemoized = query.storage {
239243
let s = format!("{trait_name}::{fn_name}");
240244
Some(quote! {
241245
let _p = tracing::debug_span!(#s, #(#key_names = tracing::field::debug(&#key_names)),*).entered();
@@ -376,8 +380,9 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
376380

377381
let storage = match &query.storage {
378382
QueryStorage::Memoized => quote!(salsa::plumbing::MemoizedStorage<Self>),
379-
QueryStorage::Dependencies => {
380-
quote!(salsa::plumbing::DependencyStorage<Self>)
383+
QueryStorage::LruMemoized => quote!(salsa::plumbing::LruMemoizedStorage<Self>),
384+
QueryStorage::LruDependencies => {
385+
quote!(salsa::plumbing::LruDependencyStorage<Self>)
381386
}
382387
QueryStorage::Input if query.keys.is_empty() => {
383388
quote!(salsa::plumbing::UnitInputStorage<Self>)
@@ -724,7 +729,8 @@ impl Query {
724729
#[derive(Debug, Clone, PartialEq, Eq)]
725730
enum QueryStorage {
726731
Memoized,
727-
Dependencies,
732+
LruDependencies,
733+
LruMemoized,
728734
Input,
729735
Interned,
730736
InternedLookup { intern_query_type: Ident },
@@ -739,7 +745,9 @@ impl QueryStorage {
739745
| QueryStorage::Interned
740746
| QueryStorage::InternedLookup { .. }
741747
| QueryStorage::Transparent => false,
742-
QueryStorage::Memoized | QueryStorage::Dependencies => true,
748+
QueryStorage::Memoized | QueryStorage::LruMemoized | QueryStorage::LruDependencies => {
749+
true
750+
}
743751
}
744752
}
745753
}

src/tools/rust-analyzer/crates/salsa/src/derived.rs

Lines changed: 11 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use crate::debug::TableEntry;
22
use crate::durability::Durability;
33
use crate::hash::FxIndexMap;
4-
use crate::lru::Lru;
54
use crate::plumbing::DerivedQueryStorageOps;
6-
use crate::plumbing::LruQueryStorageOps;
75
use crate::plumbing::QueryFunction;
86
use crate::plumbing::QueryStorageMassOps;
97
use crate::plumbing::QueryStorageOps;
@@ -13,7 +11,6 @@ use crate::{Database, DatabaseKeyIndex, QueryDb, Revision};
1311
use parking_lot::RwLock;
1412
use std::borrow::Borrow;
1513
use std::hash::Hash;
16-
use std::marker::PhantomData;
1714
use triomphe::Arc;
1815

1916
mod slot;
@@ -22,79 +19,32 @@ use slot::Slot;
2219
/// Memoized queries store the result plus a list of the other queries
2320
/// that they invoked. This means we can avoid recomputing them when
2421
/// none of those inputs have changed.
25-
pub type MemoizedStorage<Q> = DerivedStorage<Q, AlwaysMemoizeValue>;
26-
27-
/// "Dependency" queries just track their dependencies and not the
28-
/// actual value (which they produce on demand). This lessens the
29-
/// storage requirements.
30-
pub type DependencyStorage<Q> = DerivedStorage<Q, NeverMemoizeValue>;
22+
pub type MemoizedStorage<Q> = DerivedStorage<Q>;
3123

3224
/// Handles storage where the value is 'derived' by executing a
3325
/// function (in contrast to "inputs").
34-
pub struct DerivedStorage<Q, MP>
26+
pub struct DerivedStorage<Q>
3527
where
3628
Q: QueryFunction,
37-
MP: MemoizationPolicy<Q>,
3829
{
3930
group_index: u16,
40-
lru_list: Lru<Slot<Q, MP>>,
41-
slot_map: RwLock<FxIndexMap<Q::Key, Arc<Slot<Q, MP>>>>,
42-
policy: PhantomData<MP>,
31+
slot_map: RwLock<FxIndexMap<Q::Key, Arc<Slot<Q>>>>,
4332
}
4433

45-
impl<Q, MP> std::panic::RefUnwindSafe for DerivedStorage<Q, MP>
34+
impl<Q> std::panic::RefUnwindSafe for DerivedStorage<Q>
4635
where
4736
Q: QueryFunction,
48-
MP: MemoizationPolicy<Q>,
37+
4938
Q::Key: std::panic::RefUnwindSafe,
5039
Q::Value: std::panic::RefUnwindSafe,
5140
{
5241
}
5342

54-
pub trait MemoizationPolicy<Q>: Send + Sync
43+
impl<Q> DerivedStorage<Q>
5544
where
5645
Q: QueryFunction,
5746
{
58-
fn should_memoize_value(key: &Q::Key) -> bool;
59-
60-
fn memoized_value_eq(old_value: &Q::Value, new_value: &Q::Value) -> bool;
61-
}
62-
63-
pub enum AlwaysMemoizeValue {}
64-
impl<Q> MemoizationPolicy<Q> for AlwaysMemoizeValue
65-
where
66-
Q: QueryFunction,
67-
Q::Value: Eq,
68-
{
69-
fn should_memoize_value(_key: &Q::Key) -> bool {
70-
true
71-
}
72-
73-
fn memoized_value_eq(old_value: &Q::Value, new_value: &Q::Value) -> bool {
74-
old_value == new_value
75-
}
76-
}
77-
78-
pub enum NeverMemoizeValue {}
79-
impl<Q> MemoizationPolicy<Q> for NeverMemoizeValue
80-
where
81-
Q: QueryFunction,
82-
{
83-
fn should_memoize_value(_key: &Q::Key) -> bool {
84-
false
85-
}
86-
87-
fn memoized_value_eq(_old_value: &Q::Value, _new_value: &Q::Value) -> bool {
88-
panic!("cannot reach since we never memoize")
89-
}
90-
}
91-
92-
impl<Q, MP> DerivedStorage<Q, MP>
93-
where
94-
Q: QueryFunction,
95-
MP: MemoizationPolicy<Q>,
96-
{
97-
fn slot(&self, key: &Q::Key) -> Arc<Slot<Q, MP>> {
47+
fn slot(&self, key: &Q::Key) -> Arc<Slot<Q>> {
9848
if let Some(v) = self.slot_map.read().get(key) {
9949
return v.clone();
10050
}
@@ -111,20 +61,14 @@ where
11161
}
11262
}
11363

114-
impl<Q, MP> QueryStorageOps<Q> for DerivedStorage<Q, MP>
64+
impl<Q> QueryStorageOps<Q> for DerivedStorage<Q>
11565
where
11666
Q: QueryFunction,
117-
MP: MemoizationPolicy<Q>,
11867
{
11968
const CYCLE_STRATEGY: crate::plumbing::CycleRecoveryStrategy = Q::CYCLE_STRATEGY;
12069

12170
fn new(group_index: u16) -> Self {
122-
DerivedStorage {
123-
group_index,
124-
slot_map: RwLock::new(FxIndexMap::default()),
125-
lru_list: Default::default(),
126-
policy: PhantomData,
127-
}
71+
DerivedStorage { group_index, slot_map: RwLock::new(FxIndexMap::default()) }
12872
}
12973

13074
fn fmt_index(
@@ -161,10 +105,6 @@ where
161105
let slot = self.slot(key);
162106
let StampedValue { value, durability, changed_at } = slot.read(db, key);
163107

164-
if let Some(evicted) = self.lru_list.record_use(&slot) {
165-
evicted.evict();
166-
}
167-
168108
db.salsa_runtime().report_query_read_and_unwind_if_cycle_resulted(
169109
slot.database_key_index(),
170110
durability,
@@ -187,31 +127,18 @@ where
187127
}
188128
}
189129

190-
impl<Q, MP> QueryStorageMassOps for DerivedStorage<Q, MP>
130+
impl<Q> QueryStorageMassOps for DerivedStorage<Q>
191131
where
192132
Q: QueryFunction,
193-
MP: MemoizationPolicy<Q>,
194133
{
195134
fn purge(&self) {
196-
self.lru_list.purge();
197135
*self.slot_map.write() = Default::default();
198136
}
199137
}
200138

201-
impl<Q, MP> LruQueryStorageOps for DerivedStorage<Q, MP>
202-
where
203-
Q: QueryFunction,
204-
MP: MemoizationPolicy<Q>,
205-
{
206-
fn set_lru_capacity(&self, new_capacity: u16) {
207-
self.lru_list.set_lru_capacity(new_capacity);
208-
}
209-
}
210-
211-
impl<Q, MP> DerivedQueryStorageOps<Q> for DerivedStorage<Q, MP>
139+
impl<Q> DerivedQueryStorageOps<Q> for DerivedStorage<Q>
212140
where
213141
Q: QueryFunction,
214-
MP: MemoizationPolicy<Q>,
215142
{
216143
fn invalidate<S>(&self, runtime: &mut Runtime, key: &S)
217144
where

0 commit comments

Comments
 (0)