Skip to content

Commit

Permalink
Auto merge of rust-lang#17670 - Veykril:mem, r=Veykril
Browse files Browse the repository at this point in the history
LRU `body_with_source_map` query

This query is being invalidated all the time anyways (we have an extra query on top of it for the body incrementality that is not source dependent), so there is little reason to keep these around all the time when only some IDE features are interested in them.
  • Loading branch information
bors committed Jul 22, 2024
2 parents e651da9 + cdb7d3e commit be1dc79
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/tools/rust-analyzer/crates/hir-def/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
// endregion:data

#[salsa::invoke(Body::body_with_source_map_query)]
#[salsa::lru]
fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);

#[salsa::invoke(Body::body_query)]
Expand Down
14 changes: 0 additions & 14 deletions src/tools/rust-analyzer/crates/hir-expand/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ pub trait ExpandDatabase: SourceDatabase {
/// file or a macro expansion.
#[salsa::transparent]
fn parse_or_expand(&self, file_id: HirFileId) -> SyntaxNode;
#[salsa::transparent]
fn parse_or_expand_with_err(&self, file_id: HirFileId) -> ExpandResult<Parse<SyntaxNode>>;
/// Implementation for the macro case.
#[salsa::lru]
fn parse_macro_expansion(
Expand Down Expand Up @@ -328,18 +326,6 @@ fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> SyntaxNode {
}
}

fn parse_or_expand_with_err(
db: &dyn ExpandDatabase,
file_id: HirFileId,
) -> ExpandResult<Parse<SyntaxNode>> {
match file_id.repr() {
HirFileIdRepr::FileId(file_id) => ExpandResult::ok(db.parse(file_id).to_syntax()),
HirFileIdRepr::MacroFile(macro_file) => {
db.parse_macro_expansion(macro_file).map(|(it, _)| it)
}
}
}

// FIXME: We should verify that the parsed node is one of the many macro node variants we expect
// instead of having it be untyped
fn parse_macro_expansion(
Expand Down
2 changes: 2 additions & 0 deletions src/tools/rust-analyzer/crates/ide-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl RootDatabase {
// macro expansions are usually rather small, so we can afford to keep more of them alive
hir::db::ParseMacroExpansionQuery.in_db_mut(self).set_lru_capacity(4 * lru_capacity);
hir::db::BorrowckQuery.in_db_mut(self).set_lru_capacity(base_db::DEFAULT_BORROWCK_LRU_CAP);
hir::db::BodyWithSourceMapQuery.in_db_mut(self).set_lru_capacity(2048);
}

pub fn update_lru_capacities(&mut self, lru_capacities: &FxHashMap<Box<str>, u16>) {
Expand All @@ -192,6 +193,7 @@ impl RootDatabase {
.copied()
.unwrap_or(base_db::DEFAULT_BORROWCK_LRU_CAP),
);
hir::db::BodyWithSourceMapQuery.in_db_mut(self).set_lru_capacity(2048);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,10 @@ impl flags::AnalysisStats {
bar.println(msg());
}
bar.set_message(msg);
let (body, sm) = db.body_with_source_map(body_id.into());
let body = db.body(body_id.into());
let inference_result = db.infer(body_id.into());
// This query is LRU'd, so actually calling it will skew the timing results.
let sm = || db.body_with_source_map(body_id.into()).1;

// region:expressions
let (previous_exprs, previous_unknown, previous_partially_unknown) =
Expand All @@ -675,7 +677,8 @@ impl flags::AnalysisStats {
let unknown_or_partial = if ty.is_unknown() {
num_exprs_unknown += 1;
if verbosity.is_spammy() {
if let Some((path, start, end)) = expr_syntax_range(db, vfs, &sm, expr_id) {
if let Some((path, start, end)) = expr_syntax_range(db, vfs, &sm(), expr_id)
{
bar.println(format!(
"{} {}:{}-{}:{}: Unknown type",
path,
Expand All @@ -699,7 +702,7 @@ impl flags::AnalysisStats {
};
if self.only.is_some() && verbosity.is_spammy() {
// in super-verbose mode for just one function, we print every single expression
if let Some((_, start, end)) = expr_syntax_range(db, vfs, &sm, expr_id) {
if let Some((_, start, end)) = expr_syntax_range(db, vfs, &sm(), expr_id) {
bar.println(format!(
"{}:{}-{}:{}: {}",
start.line + 1,
Expand All @@ -715,14 +718,15 @@ impl flags::AnalysisStats {
if unknown_or_partial && self.output == Some(OutputFormat::Csv) {
println!(
r#"{},type,"{}""#,
location_csv_expr(db, vfs, &sm, expr_id),
location_csv_expr(db, vfs, &sm(), expr_id),
ty.display(db)
);
}
if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) {
num_expr_type_mismatches += 1;
if verbosity.is_verbose() {
if let Some((path, start, end)) = expr_syntax_range(db, vfs, &sm, expr_id) {
if let Some((path, start, end)) = expr_syntax_range(db, vfs, &sm(), expr_id)
{
bar.println(format!(
"{} {}:{}-{}:{}: Expected {}, got {}",
path,
Expand All @@ -745,7 +749,7 @@ impl flags::AnalysisStats {
if self.output == Some(OutputFormat::Csv) {
println!(
r#"{},mismatch,"{}","{}""#,
location_csv_expr(db, vfs, &sm, expr_id),
location_csv_expr(db, vfs, &sm(), expr_id),
mismatch.expected.display(db),
mismatch.actual.display(db)
);
Expand All @@ -772,7 +776,7 @@ impl flags::AnalysisStats {
let unknown_or_partial = if ty.is_unknown() {
num_pats_unknown += 1;
if verbosity.is_spammy() {
if let Some((path, start, end)) = pat_syntax_range(db, vfs, &sm, pat_id) {
if let Some((path, start, end)) = pat_syntax_range(db, vfs, &sm(), pat_id) {
bar.println(format!(
"{} {}:{}-{}:{}: Unknown type",
path,
Expand All @@ -796,7 +800,7 @@ impl flags::AnalysisStats {
};
if self.only.is_some() && verbosity.is_spammy() {
// in super-verbose mode for just one function, we print every single pattern
if let Some((_, start, end)) = pat_syntax_range(db, vfs, &sm, pat_id) {
if let Some((_, start, end)) = pat_syntax_range(db, vfs, &sm(), pat_id) {
bar.println(format!(
"{}:{}-{}:{}: {}",
start.line + 1,
Expand All @@ -812,14 +816,14 @@ impl flags::AnalysisStats {
if unknown_or_partial && self.output == Some(OutputFormat::Csv) {
println!(
r#"{},type,"{}""#,
location_csv_pat(db, vfs, &sm, pat_id),
location_csv_pat(db, vfs, &sm(), pat_id),
ty.display(db)
);
}
if let Some(mismatch) = inference_result.type_mismatch_for_pat(pat_id) {
num_pat_type_mismatches += 1;
if verbosity.is_verbose() {
if let Some((path, start, end)) = pat_syntax_range(db, vfs, &sm, pat_id) {
if let Some((path, start, end)) = pat_syntax_range(db, vfs, &sm(), pat_id) {
bar.println(format!(
"{} {}:{}-{}:{}: Expected {}, got {}",
path,
Expand All @@ -842,7 +846,7 @@ impl flags::AnalysisStats {
if self.output == Some(OutputFormat::Csv) {
println!(
r#"{},mismatch,"{}","{}""#,
location_csv_pat(db, vfs, &sm, pat_id),
location_csv_pat(db, vfs, &sm(), pat_id),
mismatch.expected.display(db),
mismatch.actual.display(db)
);
Expand Down Expand Up @@ -957,7 +961,7 @@ impl flags::AnalysisStats {
bar.println(msg());
}
bar.set_message(msg);
db.body_with_source_map(body_id.into());
db.body(body_id.into());
bar.inc(1);
}

Expand Down

0 comments on commit be1dc79

Please sign in to comment.