From a3b6e891ea7fa1f89f399183ab1bf668e1142c25 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 6 Mar 2024 19:11:40 +0100 Subject: [PATCH] Add tracing spans to macro generated database --- crates/hir-def/src/attr.rs | 2 +- crates/hir-def/src/db.rs | 13 +---- crates/hir-def/src/item_tree.rs | 2 +- crates/hir-ty/src/db.rs | 49 +------------------ crates/hir/src/db.rs | 28 +++++------ crates/ide-db/src/apply_change.rs | 4 -- crates/ide-db/src/lib.rs | 3 -- crates/salsa/salsa-macros/src/query_group.rs | 13 ++++- crates/salsa/src/derived.rs | 14 +++--- crates/salsa/tests/cycles.rs | 48 +++++++++--------- crates/salsa/tests/on_demand_inputs.rs | 26 +++++----- .../parallel/parallel_cycle_none_recover.rs | 4 +- 12 files changed, 78 insertions(+), 128 deletions(-) diff --git a/crates/hir-def/src/attr.rs b/crates/hir-def/src/attr.rs index 519706c65f..21536098b8 100644 --- a/crates/hir-def/src/attr.rs +++ b/crates/hir-def/src/attr.rs @@ -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() } } diff --git a/crates/hir-def/src/db.rs b/crates/hir-def/src/db.rs index 68f57600ec..544ed6bc34 100644 --- a/crates/hir-def/src/db.rs +++ b/crates/hir-def/src/db.rs @@ -87,14 +87,10 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast Arc; #[salsa::invoke(ItemTree::block_item_tree_query)] - fn block_item_tree_query(&self, block_id: BlockId) -> Arc; - - #[salsa::invoke(crate_def_map_wait)] - #[salsa::transparent] - fn crate_def_map(&self, krate: CrateId) -> Arc; + fn block_item_tree(&self, block_id: BlockId) -> Arc; #[salsa::invoke(DefMap::crate_def_map_query)] - fn crate_def_map_query(&self, krate: CrateId) -> Arc; + fn crate_def_map(&self, krate: CrateId) -> Arc; /// 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 { - 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()); diff --git a/crates/hir-def/src/item_tree.rs b/crates/hir-def/src/item_tree.rs index 008fa7ef13..179da788d3 100644 --- a/crates/hir-def/src/item_tree.rs +++ b/crates/hir-def/src/item_tree.rs @@ -392,7 +392,7 @@ impl TreeId { pub(crate) fn item_tree(&self, db: &dyn DefDatabase) -> Arc { 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), } } diff --git a/crates/hir-ty/src/db.rs b/crates/hir-ty/src/db.rs index f9e8cff553..28c497989f 100644 --- a/crates/hir-ty/src/db.rs +++ b/crates/hir-ty/src/db.rs @@ -31,12 +31,8 @@ use hir_expand::name::Name; #[salsa::query_group(HirDatabaseStorage)] pub trait HirDatabase: DefDatabase + Upcast { - #[salsa::invoke(infer_wait)] - #[salsa::transparent] - fn infer(&self, def: DefWithBodyId) -> Arc; - #[salsa::invoke(crate::infer::infer_query)] - fn infer_query(&self, def: DefWithBodyId) -> Arc; + fn infer(&self, def: DefWithBodyId) -> Arc; // region:mir @@ -258,17 +254,8 @@ pub trait HirDatabase: DefDatabase + Upcast { env: Arc, ) -> Ty; - #[salsa::invoke(trait_solve_wait)] - #[salsa::transparent] - fn trait_solve( - &self, - krate: CrateId, - block: Option, - goal: crate::Canonical>, - ) -> Option; - #[salsa::invoke(crate::traits::trait_solve_query)] - fn trait_solve_query( + fn trait_solve( &self, krate: CrateId, block: Option, @@ -284,38 +271,6 @@ pub trait HirDatabase: DefDatabase + Upcast { ) -> chalk_ir::ProgramClauses; } -fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc { - 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, - goal: crate::Canonical>, -) -> Option { - 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) {} diff --git a/crates/hir/src/db.rs b/crates/hir/src/db.rs index 557c8d29a1..1d74f9a4bb 100644 --- a/crates/hir/src/db.rs +++ b/crates/hir/src/db.rs @@ -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, diff --git a/crates/ide-db/src/apply_change.rs b/crates/ide-db/src/apply_change.rs index f769f72d46..017635d88e 100644 --- a/crates/ide-db/src/apply_change.rs +++ b/crates/ide-db/src/apply_change.rs @@ -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 diff --git a/crates/ide-db/src/lib.rs b/crates/ide-db/src/lib.rs index 0d11858ed7..be08b37bac 100644 --- a/crates/ide-db/src/lib.rs +++ b/crates/ide-db/src/lib.rs @@ -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 diff --git a/crates/salsa/salsa-macros/src/query_group.rs b/crates/salsa/salsa-macros/src/query_group.rs index a868d920b6..5983765eec 100644 --- a/crates/salsa/salsa-macros/src/query_group.rs +++ b/crates/salsa/salsa-macros/src/query_group.rs @@ -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),*) diff --git a/crates/salsa/src/derived.rs b/crates/salsa/src/derived.rs index 153df999f5..3b5bd7f9e3 100644 --- a/crates/salsa/src/derived.rs +++ b/crates/salsa/src/derived.rs @@ -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::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) } diff --git a/crates/salsa/tests/cycles.rs b/crates/salsa/tests/cycles.rs index 2be167bd8d..ea5d15a250 100644 --- a/crates/salsa/tests/cycles.rs +++ b/crates/salsa/tests/cycles.rs @@ -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)); diff --git a/crates/salsa/tests/on_demand_inputs.rs b/crates/salsa/tests/on_demand_inputs.rs index 677d633ee7..cad594f536 100644 --- a/crates/salsa/tests/on_demand_inputs.rs +++ b/crates/salsa/tests/on_demand_inputs.rs @@ -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); diff --git a/crates/salsa/tests/parallel/parallel_cycle_none_recover.rs b/crates/salsa/tests/parallel/parallel_cycle_none_recover.rs index e94982f513..2930c4e379 100644 --- a/crates/salsa/tests/parallel/parallel_cycle_none_recover.rs +++ b/crates/salsa/tests/parallel/parallel_cycle_none_recover.rs @@ -27,8 +27,8 @@ fn parallel_cycle_none_recover() { if let Some(c) = err_b.downcast_ref::() { 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));