Skip to content

Commit

Permalink
[red-knot] Add tracing to Salsa queries (#11949)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Jun 20, 2024
1 parent 2dfbf11 commit b456051
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
9 changes: 6 additions & 3 deletions crates/red_knot_python_semantic/src/module/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use salsa::DebugWithDb;
use std::ops::Deref;
use std::sync::Arc;

Expand Down Expand Up @@ -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<Module> {
let interned_name = internal::ModuleNameIngredient::new(db, module_name);

Expand All @@ -45,6 +45,8 @@ pub(crate) fn resolve_module_query(
db: &dyn Db,
module_name: internal::ModuleNameIngredient,
) -> Option<Module> {
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)?;
Expand Down Expand Up @@ -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`.
#[salsa::tracked]
#[tracing::instrument(level = "debug", skip(db))]
pub fn file_to_module(db: &dyn Db, file: VfsFile) -> Option<Module> {
pub(crate) 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 search_paths = module_search_paths(db);
Expand Down
6 changes: 6 additions & 0 deletions crates/red_knot_python_semantic/src/semantic_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,8 @@ type SymbolMap = hashbrown::HashMap<ScopedSymbolId, (), ()>;
/// 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()
Expand All @@ -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<SymbolTable> {
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))
Expand All @@ -48,6 +52,8 @@ pub(crate) fn symbol_table(db: &dyn Db, scope: ScopeId) -> Arc<SymbolTable> {
/// 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)
}

Expand Down
5 changes: 5 additions & 0 deletions crates/red_knot_python_semantic/src/semantic_index/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions crates/ruff_db/src/parsed.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use salsa::DebugWithDb;
use std::fmt::Formatter;
use std::ops::Deref;
use std::sync::Arc;
Expand All @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions crates/ruff_db/src/source.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use salsa::DebugWithDb;
use std::ops::Deref;
use std::sync::Arc;

Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit b456051

Please sign in to comment.