LRU body_with_source_map query

This commit is contained in:
Lukas Wirth 2024-07-22 16:34:59 +02:00
parent 232e55515f
commit bd359b32b0
4 changed files with 19 additions and 26 deletions

View file

@ -177,6 +177,7 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
// endregion:data // endregion:data
#[salsa::invoke(Body::body_with_source_map_query)] #[salsa::invoke(Body::body_with_source_map_query)]
#[salsa::lru]
fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>); fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
#[salsa::invoke(Body::body_query)] #[salsa::invoke(Body::body_query)]

View file

@ -62,8 +62,6 @@ pub trait ExpandDatabase: SourceDatabase {
/// file or a macro expansion. /// file or a macro expansion.
#[salsa::transparent] #[salsa::transparent]
fn parse_or_expand(&self, file_id: HirFileId) -> SyntaxNode; 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. /// Implementation for the macro case.
#[salsa::lru] #[salsa::lru]
fn parse_macro_expansion( fn parse_macro_expansion(
@ -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 // FIXME: We should verify that the parsed node is one of the many macro node variants we expect
// instead of having it be untyped // instead of having it be untyped
fn parse_macro_expansion( fn parse_macro_expansion(

View file

@ -168,6 +168,7 @@ impl RootDatabase {
// macro expansions are usually rather small, so we can afford to keep more of them alive // 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::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::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>) { pub fn update_lru_capacities(&mut self, lru_capacities: &FxHashMap<Box<str>, u16>) {
@ -192,6 +193,7 @@ impl RootDatabase {
.copied() .copied()
.unwrap_or(base_db::DEFAULT_BORROWCK_LRU_CAP), .unwrap_or(base_db::DEFAULT_BORROWCK_LRU_CAP),
); );
hir::db::BodyWithSourceMapQuery.in_db_mut(self).set_lru_capacity(2048);
} }
} }

View file

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