From 03e34b1403f8905b8e12c03fc7fa3d39791a12a1 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Thu, 20 Jun 2024 13:17:11 +0200 Subject: [PATCH] Add tracing to salsa queries --- crates/red_knot_python_semantic/src/module/resolver.rs | 9 ++++++--- crates/red_knot_python_semantic/src/semantic_index.rs | 6 ++++++ .../src/semantic_index/symbol.rs | 5 +++++ crates/red_knot_python_semantic/src/types.rs | 4 +++- crates/ruff_db/src/parsed.rs | 3 +++ crates/ruff_db/src/source.rs | 5 +++++ 6 files changed, 28 insertions(+), 4 deletions(-) diff --git a/crates/red_knot_python_semantic/src/module/resolver.rs b/crates/red_knot_python_semantic/src/module/resolver.rs index 8c7cf60526ac1..9275836faeb82 100644 --- a/crates/red_knot_python_semantic/src/module/resolver.rs +++ b/crates/red_knot_python_semantic/src/module/resolver.rs @@ -1,3 +1,4 @@ +use salsa::DebugWithDb; use std::ops::Deref; use std::sync::Arc; @@ -29,7 +30,6 @@ pub fn set_module_resolution_settings(db: &mut dyn Db, config: ModuleResolutionS } /// Resolves a module name to a module. -#[tracing::instrument(level = "debug", skip(db))] pub fn resolve_module(db: &dyn Db, module_name: ModuleName) -> Option { let interned_name = internal::ModuleNameIngredient::new(db, module_name); @@ -45,6 +45,8 @@ pub(crate) fn resolve_module_query( db: &dyn Db, module_name: internal::ModuleNameIngredient, ) -> Option { + let _ = tracing::trace_span!("resolve_module", module_name = ?module_name.debug(db)).enter(); + let name = module_name.name(db); let (search_path, module_file, kind) = resolve_name(db, name)?; @@ -82,8 +84,9 @@ pub fn path_to_module(db: &dyn Db, path: &VfsPath) -> Option { /// /// Returns `None` if the file is not a module locatable via `sys.path`. #[salsa::tracked] -#[tracing::instrument(level = "debug", skip(db))] -pub fn file_to_module(db: &dyn Db, file: VfsFile) -> Option { +pub(crate) fn file_to_module(db: &dyn Db, file: VfsFile) -> Option { + let _ = tracing::trace_span!("file_to_module", file = ?file.debug(db.upcast())).enter(); + let path = file.path(db.upcast()); let search_paths = module_search_paths(db); diff --git a/crates/red_knot_python_semantic/src/semantic_index.rs b/crates/red_knot_python_semantic/src/semantic_index.rs index faad4cba9f48a..13c20f3e348ca 100644 --- a/crates/red_knot_python_semantic/src/semantic_index.rs +++ b/crates/red_knot_python_semantic/src/semantic_index.rs @@ -2,6 +2,7 @@ use std::iter::FusedIterator; use std::sync::Arc; use rustc_hash::FxHashMap; +use salsa::DebugWithDb; use ruff_db::parsed::parsed_module; use ruff_db::vfs::VfsFile; @@ -28,6 +29,8 @@ type SymbolMap = hashbrown::HashMap; /// Prefer using [`symbol_table`] when working with symbols from a single scope. #[salsa::tracked(return_ref, no_eq)] pub(crate) fn semantic_index(db: &dyn Db, file: VfsFile) -> SemanticIndex { + let _ = tracing::trace_span!("semantic_index", file = ?file.debug(db.upcast())).enter(); + let parsed = parsed_module(db.upcast(), file); SemanticIndexBuilder::new(parsed).build() @@ -40,6 +43,7 @@ pub(crate) fn semantic_index(db: &dyn Db, file: VfsFile) -> SemanticIndex { /// is unchanged. #[salsa::tracked] pub(crate) fn symbol_table(db: &dyn Db, scope: ScopeId) -> Arc { + let _ = tracing::trace_span!("symbol_table", scope = ?scope.debug(db)).enter(); let index = semantic_index(db, scope.file(db)); index.symbol_table(scope.file_scope_id(db)) @@ -48,6 +52,8 @@ pub(crate) fn symbol_table(db: &dyn Db, scope: ScopeId) -> Arc { /// Returns the root scope of `file`. #[salsa::tracked] pub(crate) fn root_scope(db: &dyn Db, file: VfsFile) -> ScopeId { + let _ = tracing::trace_span!("root_scope", file = ?file.debug(db.upcast())).enter(); + FileScopeId::root().to_scope_id(db, file) } diff --git a/crates/red_knot_python_semantic/src/semantic_index/symbol.rs b/crates/red_knot_python_semantic/src/semantic_index/symbol.rs index 402a8d52ca4a9..d2498f19d2d4a 100644 --- a/crates/red_knot_python_semantic/src/semantic_index/symbol.rs +++ b/crates/red_knot_python_semantic/src/semantic_index/symbol.rs @@ -8,6 +8,7 @@ use std::ops::Range; use bitflags::bitflags; use hashbrown::hash_map::RawEntryMut; use rustc_hash::FxHasher; +use salsa::DebugWithDb; use smallvec::SmallVec; use ruff_db::vfs::VfsFile; @@ -132,6 +133,8 @@ impl ScopedSymbolId { /// Returns a mapping from [`FileScopeId`] to globally unique [`ScopeId`]. #[salsa::tracked(return_ref)] pub(crate) fn scopes_map(db: &dyn Db, file: VfsFile) -> ScopesMap { + let _ = tracing::trace_span!("scopes_map", file = ?file.debug(db.upcast())).enter(); + let index = semantic_index(db, file); let scopes: IndexVec<_, _> = index @@ -162,6 +165,8 @@ impl ScopesMap { #[salsa::tracked(return_ref)] pub(crate) fn public_symbols_map(db: &dyn Db, file: VfsFile) -> PublicSymbolsMap { + let _ = tracing::trace_span!("public_symbols_map", file = ?file.debug(db.upcast())).enter(); + let module_scope = root_scope(db, file); let symbols = symbol_table(db, module_scope); diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index 2eec55e08ab4b..991281e24b5ea 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -62,7 +62,7 @@ pub(crate) fn expression_ty(db: &dyn Db, file: VfsFile, expression: &ast::Expr) /// This being a query ensures that the invalidation short-circuits if the type of this symbol didn't change. #[salsa::tracked] pub(crate) fn public_symbol_ty(db: &dyn Db, symbol: PublicSymbolId) -> Type { - let _ = tracing::debug_span!("public_symbol_ty", symbol = ?symbol.debug(db)).enter(); + let _ = tracing::trace_span!("public_symbol_ty", symbol = ?symbol.debug(db)).enter(); let file = symbol.file(db); let scope = root_scope(db, file); @@ -80,6 +80,8 @@ pub fn public_symbol_ty_by_name(db: &dyn Db, file: VfsFile, name: &str) -> Optio /// Infers all types for `scope`. #[salsa::tracked(return_ref)] pub(crate) fn infer_types(db: &dyn Db, scope: ScopeId) -> TypeInference { + let _ = tracing::trace_span!("infer_types", scope = ?scope.debug(db)).enter(); + let file = scope.file(db); // Using the index here is fine because the code below depends on the AST anyway. // The isolation of the query is by the return inferred types. diff --git a/crates/ruff_db/src/parsed.rs b/crates/ruff_db/src/parsed.rs index 73f921dbbf03f..5808bca4ae145 100644 --- a/crates/ruff_db/src/parsed.rs +++ b/crates/ruff_db/src/parsed.rs @@ -1,3 +1,4 @@ +use salsa::DebugWithDb; use std::fmt::Formatter; use std::ops::Deref; use std::sync::Arc; @@ -22,6 +23,8 @@ use crate::Db; /// for determining if a query result is unchanged. #[salsa::tracked(return_ref, no_eq)] pub fn parsed_module(db: &dyn Db, file: VfsFile) -> ParsedModule { + let _ = tracing::trace_span!("parse_module", file = ?file.debug(db)).enter(); + let source = source_text(db, file); let path = file.path(db); diff --git a/crates/ruff_db/src/source.rs b/crates/ruff_db/src/source.rs index f7cd153d258ce..0dcab3987b72c 100644 --- a/crates/ruff_db/src/source.rs +++ b/crates/ruff_db/src/source.rs @@ -1,3 +1,4 @@ +use salsa::DebugWithDb; use std::ops::Deref; use std::sync::Arc; @@ -9,6 +10,8 @@ use crate::Db; /// Reads the content of file. #[salsa::tracked] pub fn source_text(db: &dyn Db, file: VfsFile) -> SourceText { + let _ = tracing::trace_span!("source_text", file = ?file.debug(db)).enter(); + let content = file.read(db); SourceText { @@ -19,6 +22,8 @@ pub fn source_text(db: &dyn Db, file: VfsFile) -> SourceText { /// Computes the [`LineIndex`] for `file`. #[salsa::tracked] pub fn line_index(db: &dyn Db, file: VfsFile) -> LineIndex { + let _ = tracing::trace_span!("line_index", file = ?file.debug(db)).enter(); + let source = source_text(db, file); LineIndex::from_source_text(&source)