[red-knot] Add tracing to Salsa queries (#11949)

This commit is contained in:
Micha Reiser 2024-06-20 12:33:41 +01:00 committed by GitHub
parent 2dfbf118d7
commit b456051be8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 28 additions and 4 deletions

View file

@ -1,3 +1,4 @@
use salsa::DebugWithDb;
use std::ops::Deref; use std::ops::Deref;
use std::sync::Arc; 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. /// Resolves a module name to a module.
#[tracing::instrument(level = "debug", skip(db))]
pub fn resolve_module(db: &dyn Db, module_name: ModuleName) -> Option<Module> { pub fn resolve_module(db: &dyn Db, module_name: ModuleName) -> Option<Module> {
let interned_name = internal::ModuleNameIngredient::new(db, module_name); let interned_name = internal::ModuleNameIngredient::new(db, module_name);
@ -45,6 +45,8 @@ pub(crate) fn resolve_module_query(
db: &dyn Db, db: &dyn Db,
module_name: internal::ModuleNameIngredient, module_name: internal::ModuleNameIngredient,
) -> Option<Module> { ) -> Option<Module> {
let _ = tracing::trace_span!("resolve_module", module_name = ?module_name.debug(db)).enter();
let name = module_name.name(db); let name = module_name.name(db);
let (search_path, module_file, kind) = resolve_name(db, name)?; 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<Module> {
/// ///
/// Returns `None` if the file is not a module locatable via `sys.path`. /// Returns `None` if the file is not a module locatable via `sys.path`.
#[salsa::tracked] #[salsa::tracked]
#[tracing::instrument(level = "debug", skip(db))] pub(crate) fn file_to_module(db: &dyn Db, file: VfsFile) -> Option<Module> {
pub fn file_to_module(db: &dyn Db, file: VfsFile) -> Option<Module> { let _ = tracing::trace_span!("file_to_module", file = ?file.debug(db.upcast())).enter();
let path = file.path(db.upcast()); let path = file.path(db.upcast());
let search_paths = module_search_paths(db); let search_paths = module_search_paths(db);

View file

@ -2,6 +2,7 @@ use std::iter::FusedIterator;
use std::sync::Arc; use std::sync::Arc;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use salsa::DebugWithDb;
use ruff_db::parsed::parsed_module; use ruff_db::parsed::parsed_module;
use ruff_db::vfs::VfsFile; use ruff_db::vfs::VfsFile;
@ -28,6 +29,8 @@ type SymbolMap = hashbrown::HashMap<ScopedSymbolId, (), ()>;
/// Prefer using [`symbol_table`] when working with symbols from a single scope. /// Prefer using [`symbol_table`] when working with symbols from a single scope.
#[salsa::tracked(return_ref, no_eq)] #[salsa::tracked(return_ref, no_eq)]
pub(crate) fn semantic_index(db: &dyn Db, file: VfsFile) -> SemanticIndex { 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); let parsed = parsed_module(db.upcast(), file);
SemanticIndexBuilder::new(parsed).build() SemanticIndexBuilder::new(parsed).build()
@ -40,6 +43,7 @@ pub(crate) fn semantic_index(db: &dyn Db, file: VfsFile) -> SemanticIndex {
/// is unchanged. /// is unchanged.
#[salsa::tracked] #[salsa::tracked]
pub(crate) fn symbol_table(db: &dyn Db, scope: ScopeId) -> Arc<SymbolTable> { pub(crate) fn symbol_table(db: &dyn Db, scope: ScopeId) -> Arc<SymbolTable> {
let _ = tracing::trace_span!("symbol_table", scope = ?scope.debug(db)).enter();
let index = semantic_index(db, scope.file(db)); let index = semantic_index(db, scope.file(db));
index.symbol_table(scope.file_scope_id(db)) index.symbol_table(scope.file_scope_id(db))
@ -48,6 +52,8 @@ pub(crate) fn symbol_table(db: &dyn Db, scope: ScopeId) -> Arc<SymbolTable> {
/// Returns the root scope of `file`. /// Returns the root scope of `file`.
#[salsa::tracked] #[salsa::tracked]
pub(crate) fn root_scope(db: &dyn Db, file: VfsFile) -> ScopeId { 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) FileScopeId::root().to_scope_id(db, file)
} }

View file

@ -8,6 +8,7 @@ use std::ops::Range;
use bitflags::bitflags; use bitflags::bitflags;
use hashbrown::hash_map::RawEntryMut; use hashbrown::hash_map::RawEntryMut;
use rustc_hash::FxHasher; use rustc_hash::FxHasher;
use salsa::DebugWithDb;
use smallvec::SmallVec; use smallvec::SmallVec;
use ruff_db::vfs::VfsFile; use ruff_db::vfs::VfsFile;
@ -132,6 +133,8 @@ impl ScopedSymbolId {
/// Returns a mapping from [`FileScopeId`] to globally unique [`ScopeId`]. /// Returns a mapping from [`FileScopeId`] to globally unique [`ScopeId`].
#[salsa::tracked(return_ref)] #[salsa::tracked(return_ref)]
pub(crate) fn scopes_map(db: &dyn Db, file: VfsFile) -> ScopesMap { 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 index = semantic_index(db, file);
let scopes: IndexVec<_, _> = index let scopes: IndexVec<_, _> = index
@ -162,6 +165,8 @@ impl ScopesMap {
#[salsa::tracked(return_ref)] #[salsa::tracked(return_ref)]
pub(crate) fn public_symbols_map(db: &dyn Db, file: VfsFile) -> PublicSymbolsMap { 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 module_scope = root_scope(db, file);
let symbols = symbol_table(db, module_scope); let symbols = symbol_table(db, module_scope);

View file

@ -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. /// This being a query ensures that the invalidation short-circuits if the type of this symbol didn't change.
#[salsa::tracked] #[salsa::tracked]
pub(crate) fn public_symbol_ty(db: &dyn Db, symbol: PublicSymbolId) -> Type { 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 file = symbol.file(db);
let scope = root_scope(db, file); 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`. /// Infers all types for `scope`.
#[salsa::tracked(return_ref)] #[salsa::tracked(return_ref)]
pub(crate) fn infer_types(db: &dyn Db, scope: ScopeId) -> TypeInference { 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); let file = scope.file(db);
// Using the index here is fine because the code below depends on the AST anyway. // 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. // The isolation of the query is by the return inferred types.

View file

@ -1,3 +1,4 @@
use salsa::DebugWithDb;
use std::fmt::Formatter; use std::fmt::Formatter;
use std::ops::Deref; use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
@ -22,6 +23,8 @@ use crate::Db;
/// for determining if a query result is unchanged. /// for determining if a query result is unchanged.
#[salsa::tracked(return_ref, no_eq)] #[salsa::tracked(return_ref, no_eq)]
pub fn parsed_module(db: &dyn Db, file: VfsFile) -> ParsedModule { 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 source = source_text(db, file);
let path = file.path(db); let path = file.path(db);

View file

@ -1,3 +1,4 @@
use salsa::DebugWithDb;
use std::ops::Deref; use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
@ -9,6 +10,8 @@ use crate::Db;
/// Reads the content of file. /// Reads the content of file.
#[salsa::tracked] #[salsa::tracked]
pub fn source_text(db: &dyn Db, file: VfsFile) -> SourceText { 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); let content = file.read(db);
SourceText { SourceText {
@ -19,6 +22,8 @@ pub fn source_text(db: &dyn Db, file: VfsFile) -> SourceText {
/// Computes the [`LineIndex`] for `file`. /// Computes the [`LineIndex`] for `file`.
#[salsa::tracked] #[salsa::tracked]
pub fn line_index(db: &dyn Db, file: VfsFile) -> LineIndex { 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); let source = source_text(db, file);
LineIndex::from_source_text(&source) LineIndex::from_source_text(&source)