Files
ruff/crates/ruff_python_formatter/src/statement/stmt_if.rs
Ibraheem Ahmed c9dff5c7d5 [ty] AST garbage collection (#18482)
## Summary

Garbage collect ASTs once we are done checking a given file. Queries
with a cross-file dependency on the AST will reparse the file on demand.
This reduces ty's peak memory usage by ~20-30%.

The primary change of this PR is adding a `node_index` field to every
AST node, that is assigned by the parser. `ParsedModule` can use this to
create a flat index of AST nodes any time the file is parsed (or
reparsed). This allows `AstNodeRef` to simply index into the current
instance of the `ParsedModule`, instead of storing a pointer directly.

The indices are somewhat hackily (using an atomic integer) assigned by
the `parsed_module` query instead of by the parser directly. Assigning
the indices in source-order in the (recursive) parser turns out to be
difficult, and collecting the nodes during semantic indexing is
impossible as `SemanticIndex` does not hold onto a specific
`ParsedModuleRef`, which the pointers in the flat AST are tied to. This
means that we have to do an extra AST traversal to assign and collect
the nodes into a flat index, but the small performance impact (~3% on
cold runs) seems worth it for the memory savings.

Part of https://github.com/astral-sh/ty/issues/214.
2025-06-13 08:40:11 -04:00

116 lines
3.5 KiB
Rust

use ruff_formatter::{format_args, write};
use ruff_python_ast::{AnyNodeRef, ElifElseClause, StmtIf};
use ruff_text_size::Ranged;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::statement::clause::{ClauseHeader, clause_body, clause_header};
use crate::statement::suite::SuiteKind;
#[derive(Default)]
pub struct FormatStmtIf;
impl FormatNodeRule<StmtIf> for FormatStmtIf {
fn fmt_fields(&self, item: &StmtIf, f: &mut PyFormatter) -> FormatResult<()> {
let StmtIf {
range: _,
node_index: _,
test,
body,
elif_else_clauses,
} = item;
let comments = f.context().comments().clone();
let trailing_colon_comment = comments.dangling(item);
write!(
f,
[
clause_header(
ClauseHeader::If(item),
trailing_colon_comment,
&format_args![
token("if"),
space(),
maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks),
],
),
clause_body(
body,
SuiteKind::other(elif_else_clauses.is_empty()),
trailing_colon_comment
),
]
)?;
let mut last_node = body.last().unwrap().into();
for clause in elif_else_clauses {
format_elif_else_clause(
clause,
f,
Some(last_node),
SuiteKind::other(clause == elif_else_clauses.last().unwrap()),
)?;
last_node = clause.body.last().unwrap().into();
}
Ok(())
}
}
/// Extracted so we can implement `FormatElifElseClause` but also pass in `last_node` from
/// `FormatStmtIf`
pub(crate) fn format_elif_else_clause(
item: &ElifElseClause,
f: &mut PyFormatter,
last_node: Option<AnyNodeRef>,
suite_kind: SuiteKind,
) -> FormatResult<()> {
let ElifElseClause {
range: _,
node_index: _,
test,
body,
} = item;
let comments = f.context().comments().clone();
let trailing_colon_comment = comments.dangling(item);
let leading_comments = comments.leading(item);
write!(
f,
[
clause_header(
ClauseHeader::ElifElse(item),
trailing_colon_comment,
&format_with(|f: &mut PyFormatter| {
f.options()
.source_map_generation()
.is_enabled()
.then_some(source_position(item.start()))
.fmt(f)?;
if let Some(test) = test {
write!(
f,
[
token("elif"),
space(),
maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks),
]
)
} else {
token("else").fmt(f)
}
}),
)
.with_leading_comments(leading_comments, last_node),
clause_body(body, suite_kind, trailing_colon_comment),
f.options()
.source_map_generation()
.is_enabled()
.then_some(source_position(item.end()))
]
)
}