[ty] Track heap usage of salsa structs (#19790)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Ibraheem Ahmed 2025-08-12 07:28:44 -04:00 committed by GitHub
parent 6a05d46ef6
commit f34b65b7a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 269 additions and 124 deletions

View file

@ -22,7 +22,7 @@ use crate::unpack::{Unpack, UnpackPosition};
/// because a new scope gets inserted before the `Definition` or a new place is inserted
/// before this `Definition`. However, the ID can be considered stable and it is okay to use
/// `Definition` in cross-module` salsa queries or as a field on other salsa tracked structs.
#[salsa::tracked(debug)]
#[salsa::tracked(debug, heap_size=ruff_memory_usage::heap_size)]
pub struct Definition<'db> {
/// The file in which the definition occurs.
pub file: File,
@ -645,7 +645,7 @@ impl DefinitionCategory {
/// [`DefinitionKind`] fields in salsa tracked structs should be tracked (attributed with `#[tracked]`)
/// because the kind is a thin wrapper around [`AstNodeRef`]. See the [`AstNodeRef`] documentation
/// for an in-depth explanation of why this is necessary.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, get_size2::GetSize)]
pub enum DefinitionKind<'db> {
Import(ImportDefinitionKind),
ImportFrom(ImportFromDefinitionKind),
@ -834,7 +834,7 @@ impl DefinitionKind<'_> {
}
}
#[derive(Copy, Clone, Debug, PartialEq, Hash)]
#[derive(Copy, Clone, Debug, PartialEq, Hash, get_size2::GetSize)]
pub(crate) enum TargetKind<'db> {
Sequence(UnpackPosition, Unpack<'db>),
/// Name, attribute, or subscript.
@ -850,7 +850,7 @@ impl<'db> From<Option<(UnpackPosition, Unpack<'db>)>> for TargetKind<'db> {
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, get_size2::GetSize)]
pub struct StarImportDefinitionKind {
node: AstNodeRef<ast::StmtImportFrom>,
symbol_id: ScopedSymbolId,
@ -880,7 +880,7 @@ impl StarImportDefinitionKind {
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, get_size2::GetSize)]
pub struct MatchPatternDefinitionKind {
pattern: AstNodeRef<ast::Pattern>,
identifier: AstNodeRef<ast::Identifier>,
@ -902,7 +902,7 @@ impl MatchPatternDefinitionKind {
/// But if the target is an attribute or subscript, its definition is not in the comprehension's scope;
/// it is in the scope in which the root variable is bound.
/// TODO: currently we don't model this correctly and simply assume that it is in a scope outside the comprehension.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, get_size2::GetSize)]
pub struct ComprehensionDefinitionKind<'db> {
target_kind: TargetKind<'db>,
iterable: AstNodeRef<ast::Expr>,
@ -933,7 +933,7 @@ impl<'db> ComprehensionDefinitionKind<'db> {
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, get_size2::GetSize)]
pub struct ImportDefinitionKind {
node: AstNodeRef<ast::StmtImport>,
alias_index: usize,
@ -954,7 +954,7 @@ impl ImportDefinitionKind {
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, get_size2::GetSize)]
pub struct ImportFromDefinitionKind {
node: AstNodeRef<ast::StmtImportFrom>,
alias_index: usize,
@ -975,7 +975,7 @@ impl ImportFromDefinitionKind {
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, get_size2::GetSize)]
pub struct AssignmentDefinitionKind<'db> {
target_kind: TargetKind<'db>,
value: AstNodeRef<ast::Expr>,
@ -996,7 +996,7 @@ impl<'db> AssignmentDefinitionKind<'db> {
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, get_size2::GetSize)]
pub struct AnnotatedAssignmentDefinitionKind {
annotation: AstNodeRef<ast::Expr>,
value: Option<AstNodeRef<ast::Expr>>,
@ -1017,7 +1017,7 @@ impl AnnotatedAssignmentDefinitionKind {
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, get_size2::GetSize)]
pub struct WithItemDefinitionKind<'db> {
target_kind: TargetKind<'db>,
context_expr: AstNodeRef<ast::Expr>,
@ -1043,7 +1043,7 @@ impl<'db> WithItemDefinitionKind<'db> {
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, get_size2::GetSize)]
pub struct ForStmtDefinitionKind<'db> {
target_kind: TargetKind<'db>,
iterable: AstNodeRef<ast::Expr>,
@ -1069,7 +1069,7 @@ impl<'db> ForStmtDefinitionKind<'db> {
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, get_size2::GetSize)]
pub struct ExceptHandlerDefinitionKind {
handler: AstNodeRef<ast::ExceptHandlerExceptHandler>,
is_star: bool,

View file

@ -10,7 +10,7 @@ use salsa;
/// a type expression. For example, in `self.x: <annotation> = <value>`, the
/// `<annotation>` is inferred as a type expression, while `<value>` is inferred
/// as a normal expression.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, get_size2::GetSize)]
pub(crate) enum ExpressionKind {
Normal,
TypeExpression,
@ -31,7 +31,7 @@ pub(crate) enum ExpressionKind {
/// * a return type of a cross-module query
/// * a field of a type that is a return type of a cross-module query
/// * an argument of a cross-module query
#[salsa::tracked(debug)]
#[salsa::tracked(debug, heap_size=ruff_memory_usage::heap_size)]
pub(crate) struct Expression<'db> {
/// The file in which the expression occurs.
pub(crate) file: File,

View file

@ -117,7 +117,7 @@ pub(crate) enum PredicateNode<'db> {
StarImportPlaceholder(StarImportPlaceholderPredicate<'db>),
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, salsa::Update)]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, salsa::Update, get_size2::GetSize)]
pub(crate) enum ClassPatternKind {
Irrefutable,
Refutable,
@ -130,7 +130,7 @@ impl ClassPatternKind {
}
/// Pattern kinds for which we support type narrowing and/or static reachability analysis.
#[derive(Debug, Clone, Hash, PartialEq, salsa::Update)]
#[derive(Debug, Clone, Hash, PartialEq, salsa::Update, get_size2::GetSize)]
pub(crate) enum PatternPredicateKind<'db> {
Singleton(Singleton),
Value(Expression<'db>),
@ -140,7 +140,7 @@ pub(crate) enum PatternPredicateKind<'db> {
Unsupported,
}
#[salsa::tracked(debug)]
#[salsa::tracked(debug, heap_size=ruff_memory_usage::heap_size)]
pub(crate) struct PatternPredicate<'db> {
pub(crate) file: File,
@ -206,7 +206,7 @@ impl<'db> PatternPredicate<'db> {
/// - If it resolves to a possibly bound symbol, then the predicate resolves to [`Truthiness::Ambiguous`]
///
/// [Truthiness]: [crate::types::Truthiness]
#[salsa::tracked(debug)]
#[salsa::tracked(debug, heap_size=ruff_memory_usage::heap_size)]
pub(crate) struct StarImportPlaceholderPredicate<'db> {
pub(crate) importing_file: File,

View file

@ -14,7 +14,7 @@ use crate::{
};
/// A cross-module identifier of a scope that can be used as a salsa query parameter.
#[salsa::tracked(debug)]
#[salsa::tracked(debug, heap_size=ruff_memory_usage::heap_size)]
pub struct ScopeId<'db> {
pub file: File,