Skip to content

Commit b456051

Browse files
authored
[red-knot] Add tracing to Salsa queries (#11949)
1 parent 2dfbf11 commit b456051

File tree

6 files changed

+28
-4
lines changed

6 files changed

+28
-4
lines changed

crates/red_knot_python_semantic/src/module/resolver.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use salsa::DebugWithDb;
12
use std::ops::Deref;
23
use std::sync::Arc;
34

@@ -29,7 +30,6 @@ pub fn set_module_resolution_settings(db: &mut dyn Db, config: ModuleResolutionS
2930
}
3031

3132
/// Resolves a module name to a module.
32-
#[tracing::instrument(level = "debug", skip(db))]
3333
pub fn resolve_module(db: &dyn Db, module_name: ModuleName) -> Option<Module> {
3434
let interned_name = internal::ModuleNameIngredient::new(db, module_name);
3535

@@ -45,6 +45,8 @@ pub(crate) fn resolve_module_query(
4545
db: &dyn Db,
4646
module_name: internal::ModuleNameIngredient,
4747
) -> Option<Module> {
48+
let _ = tracing::trace_span!("resolve_module", module_name = ?module_name.debug(db)).enter();
49+
4850
let name = module_name.name(db);
4951

5052
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> {
8284
///
8385
/// Returns `None` if the file is not a module locatable via `sys.path`.
8486
#[salsa::tracked]
85-
#[tracing::instrument(level = "debug", skip(db))]
86-
pub fn file_to_module(db: &dyn Db, file: VfsFile) -> Option<Module> {
87+
pub(crate) fn file_to_module(db: &dyn Db, file: VfsFile) -> Option<Module> {
88+
let _ = tracing::trace_span!("file_to_module", file = ?file.debug(db.upcast())).enter();
89+
8790
let path = file.path(db.upcast());
8891

8992
let search_paths = module_search_paths(db);

crates/red_knot_python_semantic/src/semantic_index.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::iter::FusedIterator;
22
use std::sync::Arc;
33

44
use rustc_hash::FxHashMap;
5+
use salsa::DebugWithDb;
56

67
use ruff_db::parsed::parsed_module;
78
use ruff_db::vfs::VfsFile;
@@ -28,6 +29,8 @@ type SymbolMap = hashbrown::HashMap<ScopedSymbolId, (), ()>;
2829
/// Prefer using [`symbol_table`] when working with symbols from a single scope.
2930
#[salsa::tracked(return_ref, no_eq)]
3031
pub(crate) fn semantic_index(db: &dyn Db, file: VfsFile) -> SemanticIndex {
32+
let _ = tracing::trace_span!("semantic_index", file = ?file.debug(db.upcast())).enter();
33+
3134
let parsed = parsed_module(db.upcast(), file);
3235

3336
SemanticIndexBuilder::new(parsed).build()
@@ -40,6 +43,7 @@ pub(crate) fn semantic_index(db: &dyn Db, file: VfsFile) -> SemanticIndex {
4043
/// is unchanged.
4144
#[salsa::tracked]
4245
pub(crate) fn symbol_table(db: &dyn Db, scope: ScopeId) -> Arc<SymbolTable> {
46+
let _ = tracing::trace_span!("symbol_table", scope = ?scope.debug(db)).enter();
4347
let index = semantic_index(db, scope.file(db));
4448

4549
index.symbol_table(scope.file_scope_id(db))
@@ -48,6 +52,8 @@ pub(crate) fn symbol_table(db: &dyn Db, scope: ScopeId) -> Arc<SymbolTable> {
4852
/// Returns the root scope of `file`.
4953
#[salsa::tracked]
5054
pub(crate) fn root_scope(db: &dyn Db, file: VfsFile) -> ScopeId {
55+
let _ = tracing::trace_span!("root_scope", file = ?file.debug(db.upcast())).enter();
56+
5157
FileScopeId::root().to_scope_id(db, file)
5258
}
5359

crates/red_knot_python_semantic/src/semantic_index/symbol.rs

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::ops::Range;
88
use bitflags::bitflags;
99
use hashbrown::hash_map::RawEntryMut;
1010
use rustc_hash::FxHasher;
11+
use salsa::DebugWithDb;
1112
use smallvec::SmallVec;
1213

1314
use ruff_db::vfs::VfsFile;
@@ -132,6 +133,8 @@ impl ScopedSymbolId {
132133
/// Returns a mapping from [`FileScopeId`] to globally unique [`ScopeId`].
133134
#[salsa::tracked(return_ref)]
134135
pub(crate) fn scopes_map(db: &dyn Db, file: VfsFile) -> ScopesMap {
136+
let _ = tracing::trace_span!("scopes_map", file = ?file.debug(db.upcast())).enter();
137+
135138
let index = semantic_index(db, file);
136139

137140
let scopes: IndexVec<_, _> = index
@@ -162,6 +165,8 @@ impl ScopesMap {
162165

163166
#[salsa::tracked(return_ref)]
164167
pub(crate) fn public_symbols_map(db: &dyn Db, file: VfsFile) -> PublicSymbolsMap {
168+
let _ = tracing::trace_span!("public_symbols_map", file = ?file.debug(db.upcast())).enter();
169+
165170
let module_scope = root_scope(db, file);
166171
let symbols = symbol_table(db, module_scope);
167172

crates/red_knot_python_semantic/src/types.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(crate) fn expression_ty(db: &dyn Db, file: VfsFile, expression: &ast::Expr)
6262
/// This being a query ensures that the invalidation short-circuits if the type of this symbol didn't change.
6363
#[salsa::tracked]
6464
pub(crate) fn public_symbol_ty(db: &dyn Db, symbol: PublicSymbolId) -> Type {
65-
let _ = tracing::debug_span!("public_symbol_ty", symbol = ?symbol.debug(db)).enter();
65+
let _ = tracing::trace_span!("public_symbol_ty", symbol = ?symbol.debug(db)).enter();
6666

6767
let file = symbol.file(db);
6868
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
8080
/// Infers all types for `scope`.
8181
#[salsa::tracked(return_ref)]
8282
pub(crate) fn infer_types(db: &dyn Db, scope: ScopeId) -> TypeInference {
83+
let _ = tracing::trace_span!("infer_types", scope = ?scope.debug(db)).enter();
84+
8385
let file = scope.file(db);
8486
// Using the index here is fine because the code below depends on the AST anyway.
8587
// The isolation of the query is by the return inferred types.

crates/ruff_db/src/parsed.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use salsa::DebugWithDb;
12
use std::fmt::Formatter;
23
use std::ops::Deref;
34
use std::sync::Arc;
@@ -22,6 +23,8 @@ use crate::Db;
2223
/// for determining if a query result is unchanged.
2324
#[salsa::tracked(return_ref, no_eq)]
2425
pub fn parsed_module(db: &dyn Db, file: VfsFile) -> ParsedModule {
26+
let _ = tracing::trace_span!("parse_module", file = ?file.debug(db)).enter();
27+
2528
let source = source_text(db, file);
2629
let path = file.path(db);
2730

crates/ruff_db/src/source.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use salsa::DebugWithDb;
12
use std::ops::Deref;
23
use std::sync::Arc;
34

@@ -9,6 +10,8 @@ use crate::Db;
910
/// Reads the content of file.
1011
#[salsa::tracked]
1112
pub fn source_text(db: &dyn Db, file: VfsFile) -> SourceText {
13+
let _ = tracing::trace_span!("source_text", file = ?file.debug(db)).enter();
14+
1215
let content = file.read(db);
1316

1417
SourceText {
@@ -19,6 +22,8 @@ pub fn source_text(db: &dyn Db, file: VfsFile) -> SourceText {
1922
/// Computes the [`LineIndex`] for `file`.
2023
#[salsa::tracked]
2124
pub fn line_index(db: &dyn Db, file: VfsFile) -> LineIndex {
25+
let _ = tracing::trace_span!("line_index", file = ?file.debug(db)).enter();
26+
2227
let source = source_text(db, file);
2328

2429
LineIndex::from_source_text(&source)

0 commit comments

Comments
 (0)