Add tracing spans to macro generated database

This commit is contained in:
Lukas Wirth 2024-03-06 19:11:40 +01:00
parent 52d8ae791d
commit a3b6e891ea
12 changed files with 78 additions and 128 deletions

View file

@ -348,7 +348,7 @@ impl AttrsWithOwner {
.raw_attrs(AttrOwner::ModItem(definition_tree_id.value.into()))
.clone(),
ModuleOrigin::BlockExpr { id, .. } => {
let tree = db.block_item_tree_query(id);
let tree = db.block_item_tree(id);
tree.raw_attrs(AttrOwner::TopLevel).clone()
}
}

View file

@ -87,14 +87,10 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
fn file_item_tree(&self, file_id: HirFileId) -> Arc<ItemTree>;
#[salsa::invoke(ItemTree::block_item_tree_query)]
fn block_item_tree_query(&self, block_id: BlockId) -> Arc<ItemTree>;
#[salsa::invoke(crate_def_map_wait)]
#[salsa::transparent]
fn crate_def_map(&self, krate: CrateId) -> Arc<DefMap>;
fn block_item_tree(&self, block_id: BlockId) -> Arc<ItemTree>;
#[salsa::invoke(DefMap::crate_def_map_query)]
fn crate_def_map_query(&self, krate: CrateId) -> Arc<DefMap>;
fn crate_def_map(&self, krate: CrateId) -> Arc<DefMap>;
/// Computes the block-level `DefMap`.
#[salsa::invoke(DefMap::block_def_map_query)]
@ -253,11 +249,6 @@ fn include_macro_invoc(db: &dyn DefDatabase, krate: CrateId) -> Vec<(MacroCallId
.collect()
}
fn crate_def_map_wait(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
let _p = tracing::span!(tracing::Level::INFO, "crate_def_map:wait").entered();
db.crate_def_map_query(krate)
}
fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: CrateId) -> bool {
let file = db.crate_graph()[crate_id].root_file_id;
let item_tree = db.file_item_tree(file.into());

View file

@ -392,7 +392,7 @@ impl TreeId {
pub(crate) fn item_tree(&self, db: &dyn DefDatabase) -> Arc<ItemTree> {
match self.block {
Some(block) => db.block_item_tree_query(block),
Some(block) => db.block_item_tree(block),
None => db.file_item_tree(self.file),
}
}

View file

@ -31,12 +31,8 @@ use hir_expand::name::Name;
#[salsa::query_group(HirDatabaseStorage)]
pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
#[salsa::invoke(infer_wait)]
#[salsa::transparent]
fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
#[salsa::invoke(crate::infer::infer_query)]
fn infer_query(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
// region:mir
@ -258,17 +254,8 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
env: Arc<TraitEnvironment>,
) -> Ty;
#[salsa::invoke(trait_solve_wait)]
#[salsa::transparent]
fn trait_solve(
&self,
krate: CrateId,
block: Option<BlockId>,
goal: crate::Canonical<crate::InEnvironment<crate::Goal>>,
) -> Option<crate::Solution>;
#[salsa::invoke(crate::traits::trait_solve_query)]
fn trait_solve_query(
fn trait_solve(
&self,
krate: CrateId,
block: Option<BlockId>,
@ -284,38 +271,6 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
) -> chalk_ir::ProgramClauses<Interner>;
}
fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
let detail = match def {
DefWithBodyId::FunctionId(it) => db.function_data(it).name.display(db.upcast()).to_string(),
DefWithBodyId::StaticId(it) => {
db.static_data(it).name.clone().display(db.upcast()).to_string()
}
DefWithBodyId::ConstId(it) => db
.const_data(it)
.name
.clone()
.unwrap_or_else(Name::missing)
.display(db.upcast())
.to_string(),
DefWithBodyId::VariantId(it) => {
db.enum_variant_data(it).name.display(db.upcast()).to_string()
}
DefWithBodyId::InTypeConstId(it) => format!("in type const {it:?}"),
};
let _p = tracing::span!(tracing::Level::INFO, "infer:wait", ?detail).entered();
db.infer_query(def)
}
fn trait_solve_wait(
db: &dyn HirDatabase,
krate: CrateId,
block: Option<BlockId>,
goal: crate::Canonical<crate::InEnvironment<crate::Goal>>,
) -> Option<crate::Solution> {
let _p = tracing::span!(tracing::Level::INFO, "trait_solve::wait").entered();
db.trait_solve_query(krate, block, goal)
}
#[test]
fn hir_database_is_object_safe() {
fn _assert_object_safe(_: &dyn HirDatabase) {}

View file

@ -4,20 +4,20 @@
//!
//! But we need this for at least LRU caching at the query level.
pub use hir_def::db::{
AttrsQuery, BlockDefMapQuery, BlockItemTreeQueryQuery, BodyQuery, BodyWithSourceMapQuery,
ConstDataQuery, ConstVisibilityQuery, CrateDefMapQueryQuery, CrateLangItemsQuery,
CrateSupportsNoStdQuery, DefDatabase, DefDatabaseStorage, EnumDataQuery,
EnumVariantDataWithDiagnosticsQuery, ExprScopesQuery, ExternCrateDeclDataQuery,
FieldVisibilitiesQuery, FieldsAttrsQuery, FieldsAttrsSourceMapQuery, FileItemTreeQuery,
FunctionDataQuery, FunctionVisibilityQuery, GenericParamsQuery, ImplDataWithDiagnosticsQuery,
ImportMapQuery, InternAnonymousConstQuery, InternBlockQuery, InternConstQuery, InternDatabase,
InternDatabaseStorage, InternEnumQuery, InternExternBlockQuery, InternExternCrateQuery,
InternFunctionQuery, InternImplQuery, InternInTypeConstQuery, InternMacro2Query,
InternMacroRulesQuery, InternProcMacroQuery, InternStaticQuery, InternStructQuery,
InternTraitAliasQuery, InternTraitQuery, InternTypeAliasQuery, InternUnionQuery,
InternUseQuery, LangItemQuery, Macro2DataQuery, MacroRulesDataQuery, ProcMacroDataQuery,
StaticDataQuery, StructDataWithDiagnosticsQuery, TraitAliasDataQuery,
TraitDataWithDiagnosticsQuery, TypeAliasDataQuery, UnionDataWithDiagnosticsQuery,
AttrsQuery, BlockDefMapQuery, BodyQuery, BodyWithSourceMapQuery, ConstDataQuery,
ConstVisibilityQuery, CrateLangItemsQuery, CrateSupportsNoStdQuery, DefDatabase,
DefDatabaseStorage, EnumDataQuery, EnumVariantDataWithDiagnosticsQuery, ExprScopesQuery,
ExternCrateDeclDataQuery, FieldVisibilitiesQuery, FieldsAttrsQuery, FieldsAttrsSourceMapQuery,
FileItemTreeQuery, FunctionDataQuery, FunctionVisibilityQuery, GenericParamsQuery,
ImplDataWithDiagnosticsQuery, ImportMapQuery, InternAnonymousConstQuery, InternBlockQuery,
InternConstQuery, InternDatabase, InternDatabaseStorage, InternEnumQuery,
InternExternBlockQuery, InternExternCrateQuery, InternFunctionQuery, InternImplQuery,
InternInTypeConstQuery, InternMacro2Query, InternMacroRulesQuery, InternProcMacroQuery,
InternStaticQuery, InternStructQuery, InternTraitAliasQuery, InternTraitQuery,
InternTypeAliasQuery, InternUnionQuery, InternUseQuery, LangItemQuery, Macro2DataQuery,
MacroRulesDataQuery, ProcMacroDataQuery, StaticDataQuery, StructDataWithDiagnosticsQuery,
TraitAliasDataQuery, TraitDataWithDiagnosticsQuery, TypeAliasDataQuery,
UnionDataWithDiagnosticsQuery,
};
pub use hir_expand::db::{
AstIdMapQuery, DeclMacroExpanderQuery, ExpandDatabase, ExpandDatabaseStorage,

View file

@ -91,7 +91,6 @@ impl RootDatabase {
crate::symbol_index::LocalRootsQuery
crate::symbol_index::LibraryRootsQuery
// HirDatabase
hir::db::InferQueryQuery
hir::db::MirBodyQuery
hir::db::BorrowckQuery
hir::db::TyQuery
@ -130,12 +129,10 @@ impl RootDatabase {
hir::db::FnDefVarianceQuery
hir::db::AdtVarianceQuery
hir::db::AssociatedTyValueQuery
hir::db::TraitSolveQueryQuery
hir::db::ProgramClausesForChalkEnvQuery
// DefDatabase
hir::db::FileItemTreeQuery
hir::db::CrateDefMapQueryQuery
hir::db::BlockDefMapQuery
hir::db::StructDataWithDiagnosticsQuery
hir::db::UnionDataWithDiagnosticsQuery
@ -165,7 +162,6 @@ impl RootDatabase {
hir::db::FunctionVisibilityQuery
hir::db::ConstVisibilityQuery
hir::db::CrateSupportsNoStdQuery
hir::db::BlockItemTreeQueryQuery
hir::db::ExternCrateDeclDataQuery
hir::db::InternAnonymousConstQuery
hir::db::InternExternCrateQuery

View file

@ -216,7 +216,6 @@ impl RootDatabase {
// DefDatabase
hir_db::FileItemTreeQuery
hir_db::CrateDefMapQueryQuery
hir_db::BlockDefMapQuery
hir_db::StructDataWithDiagnosticsQuery
hir_db::UnionDataWithDiagnosticsQuery
@ -248,7 +247,6 @@ impl RootDatabase {
hir_db::CrateSupportsNoStdQuery
// HirDatabase
hir_db::InferQueryQuery
hir_db::MirBodyQuery
hir_db::BorrowckQuery
hir_db::TyQuery
@ -287,7 +285,6 @@ impl RootDatabase {
hir_db::FnDefVarianceQuery
hir_db::AdtVarianceQuery
hir_db::AssociatedTyValueQuery
hir_db::TraitSolveQueryQuery
hir_db::ProgramClausesForChalkEnvQuery
// SymbolsDatabase

View file

@ -235,13 +235,24 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
queries_with_storage.push(fn_name);
let tracing = if let QueryStorage::Memoized = query.storage {
let s = format!("{trait_name}::{fn_name}");
Some(quote! {
let _p = tracing::span!(tracing::Level::DEBUG, #s, #(#key_names = tracing::field::debug(&#key_names)),*).entered();
})
} else {
None
}
.into_iter();
query_fn_definitions.extend(quote! {
fn #fn_name(&self, #(#key_names: #keys),*) -> #value {
#(#tracing),*
// Create a shim to force the code to be monomorphized in the
// query crate. Our experiments revealed that this makes a big
// difference in total compilation time in rust-analyzer, though
// it's not totally obvious why that should be.
fn __shim(db: &(dyn #trait_name + '_), #(#key_names: #keys),*) -> #value {
fn __shim(db: &(dyn #trait_name + '_), #(#key_names: #keys),*) -> #value {
salsa::plumbing::get_query_table::<#qt>(db).get((#(#key_names),*))
}
__shim(self, #(#key_names),*)

View file

@ -136,7 +136,7 @@ where
) -> std::fmt::Result {
let slot_map = self.slot_map.read();
let key = slot_map.get_index(index as usize).unwrap().0;
write!(fmt, "{}({:?})", Q::QUERY_NAME, key)
write!(fmt, "{}::{}({:?})", std::any::type_name::<Q>(), Q::QUERY_NAME, key)
}
fn maybe_changed_after(
@ -146,13 +146,13 @@ where
revision: Revision,
) -> bool {
debug_assert!(revision < db.salsa_runtime().current_revision());
let read = self.slot_map.read();
let Some((key, slot)) = read.get_index(index as usize) else {
return false;
let (key, slot) = {
let read = self.slot_map.read();
let Some((key, slot)) = read.get_index(index as usize) else {
return false;
};
(key.clone(), slot.clone())
};
let (key, slot) = (key.clone(), slot.clone());
// note: this drop is load-bearing. removing it would causes deadlocks.
drop(read);
slot.maybe_changed_after(db, revision, &key)
}

View file

@ -171,8 +171,8 @@ fn cycle_memoized() {
let cycle = extract_cycle(|| db.memoized_a());
expect![[r#"
[
"memoized_a(())",
"memoized_b(())",
"cycles::MemoizedAQuery::memoized_a(())",
"cycles::MemoizedBQuery::memoized_b(())",
]
"#]]
.assert_debug_eq(&cycle.unexpected_participants(&db));
@ -184,8 +184,8 @@ fn cycle_volatile() {
let cycle = extract_cycle(|| db.volatile_a());
expect![[r#"
[
"volatile_a(())",
"volatile_b(())",
"cycles::VolatileAQuery::volatile_a(())",
"cycles::VolatileBQuery::volatile_b(())",
]
"#]]
.assert_debug_eq(&cycle.unexpected_participants(&db));
@ -222,8 +222,8 @@ fn inner_cycle() {
let cycle = err.unwrap_err().cycle;
expect![[r#"
[
"cycle_a(())",
"cycle_b(())",
"cycles::CycleAQuery::cycle_a(())",
"cycles::CycleBQuery::cycle_b(())",
]
"#]]
.assert_debug_eq(&cycle);
@ -262,8 +262,8 @@ fn cycle_revalidate_unchanged_twice() {
Err(
Error {
cycle: [
"cycle_a(())",
"cycle_b(())",
"cycles::CycleAQuery::cycle_a(())",
"cycles::CycleBQuery::cycle_b(())",
],
},
)
@ -344,8 +344,8 @@ fn cycle_mixed_1() {
Err(
Error {
cycle: [
"cycle_b(())",
"cycle_c(())",
"cycles::CycleBQuery::cycle_b(())",
"cycles::CycleCQuery::cycle_c(())",
],
},
)
@ -371,9 +371,9 @@ fn cycle_mixed_2() {
Err(
Error {
cycle: [
"cycle_a(())",
"cycle_b(())",
"cycle_c(())",
"cycles::CycleAQuery::cycle_a(())",
"cycles::CycleBQuery::cycle_b(())",
"cycles::CycleCQuery::cycle_c(())",
],
},
)
@ -400,16 +400,16 @@ fn cycle_deterministic_order() {
Err(
Error {
cycle: [
"cycle_a(())",
"cycle_b(())",
"cycles::CycleAQuery::cycle_a(())",
"cycles::CycleBQuery::cycle_b(())",
],
},
),
Err(
Error {
cycle: [
"cycle_a(())",
"cycle_b(())",
"cycles::CycleAQuery::cycle_a(())",
"cycles::CycleBQuery::cycle_b(())",
],
},
),
@ -445,24 +445,24 @@ fn cycle_multiple() {
Err(
Error {
cycle: [
"cycle_a(())",
"cycle_b(())",
"cycles::CycleAQuery::cycle_a(())",
"cycles::CycleBQuery::cycle_b(())",
],
},
),
Err(
Error {
cycle: [
"cycle_a(())",
"cycle_b(())",
"cycles::CycleAQuery::cycle_a(())",
"cycles::CycleBQuery::cycle_b(())",
],
},
),
Err(
Error {
cycle: [
"cycle_a(())",
"cycle_b(())",
"cycles::CycleAQuery::cycle_a(())",
"cycles::CycleBQuery::cycle_b(())",
],
},
),
@ -485,7 +485,7 @@ fn cycle_recovery_set_but_not_participating() {
let r = extract_cycle(|| drop(db.cycle_a()));
expect![[r#"
[
"cycle_c(())",
"cycles::CycleCQuery::cycle_c(())",
]
"#]]
.assert_debug_eq(&r.all_participants(&db));

View file

@ -103,10 +103,10 @@ fn on_demand_input_durability() {
expect_test::expect![[r#"
RefCell {
value: [
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: b(1) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: a(1) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: b(2) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: a(2) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: on_demand_inputs::BQuery::b(1) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: on_demand_inputs::AQuery::a(1) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: on_demand_inputs::BQuery::b(2) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: on_demand_inputs::AQuery::a(2) } }",
],
}
"#]].assert_debug_eq(&events);
@ -119,11 +119,11 @@ fn on_demand_input_durability() {
expect_test::expect![[r#"
RefCell {
value: [
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: c(1) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: DidValidateMemoizedValue { database_key: b(1) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: c(2) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: a(2) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: DidValidateMemoizedValue { database_key: b(2) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: on_demand_inputs::CQuery::c(1) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: DidValidateMemoizedValue { database_key: on_demand_inputs::BQuery::b(1) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: on_demand_inputs::CQuery::c(2) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: on_demand_inputs::AQuery::a(2) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: DidValidateMemoizedValue { database_key: on_demand_inputs::BQuery::b(2) } }",
],
}
"#]].assert_debug_eq(&events);
@ -137,10 +137,10 @@ fn on_demand_input_durability() {
expect_test::expect![[r#"
RefCell {
value: [
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: a(1) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: DidValidateMemoizedValue { database_key: c(1) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: a(2) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: DidValidateMemoizedValue { database_key: c(2) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: on_demand_inputs::AQuery::a(1) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: DidValidateMemoizedValue { database_key: on_demand_inputs::CQuery::c(1) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: on_demand_inputs::AQuery::a(2) } }",
"Event { runtime_id: RuntimeId { counter: 0 }, kind: DidValidateMemoizedValue { database_key: on_demand_inputs::CQuery::c(2) } }",
],
}
"#]].assert_debug_eq(&events);

View file

@ -27,8 +27,8 @@ fn parallel_cycle_none_recover() {
if let Some(c) = err_b.downcast_ref::<salsa::Cycle>() {
expect![[r#"
[
"a(-1)",
"b(-1)",
"parallel::parallel_cycle_none_recover::AQuery::a(-1)",
"parallel::parallel_cycle_none_recover::BQuery::b(-1)",
]
"#]]
.assert_debug_eq(&c.unexpected_participants(&db));