mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-17 18:15:21 +00:00

## 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.
286 lines
11 KiB
Text
286 lines
11 KiB
Text
---
|
|
source: crates/ruff_python_parser/tests/fixtures.rs
|
|
input_file: crates/ruff_python_parser/resources/valid/statement/simple.py
|
|
---
|
|
## AST
|
|
|
|
```
|
|
Module(
|
|
ModModule {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 0..172,
|
|
body: [
|
|
Continue(
|
|
StmtContinue {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 61..69,
|
|
},
|
|
),
|
|
Break(
|
|
StmtBreak {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 70..75,
|
|
},
|
|
),
|
|
If(
|
|
StmtIf {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 77..86,
|
|
test: Name(
|
|
ExprName {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 80..81,
|
|
id: Name("x"),
|
|
ctx: Load,
|
|
},
|
|
),
|
|
body: [
|
|
Expr(
|
|
StmtExpr {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 83..86,
|
|
value: EllipsisLiteral(
|
|
ExprEllipsisLiteral {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 83..86,
|
|
},
|
|
),
|
|
},
|
|
),
|
|
],
|
|
elif_else_clauses: [],
|
|
},
|
|
),
|
|
If(
|
|
StmtIf {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 87..100,
|
|
test: BooleanLiteral(
|
|
ExprBooleanLiteral {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 90..94,
|
|
value: true,
|
|
},
|
|
),
|
|
body: [
|
|
Pass(
|
|
StmtPass {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 96..100,
|
|
},
|
|
),
|
|
],
|
|
elif_else_clauses: [],
|
|
},
|
|
),
|
|
Expr(
|
|
StmtExpr {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 101..102,
|
|
value: NumberLiteral(
|
|
ExprNumberLiteral {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 101..102,
|
|
value: Int(
|
|
1,
|
|
),
|
|
},
|
|
),
|
|
},
|
|
),
|
|
Expr(
|
|
StmtExpr {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 104..105,
|
|
value: NumberLiteral(
|
|
ExprNumberLiteral {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 104..105,
|
|
value: Int(
|
|
2,
|
|
),
|
|
},
|
|
),
|
|
},
|
|
),
|
|
Pass(
|
|
StmtPass {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 107..111,
|
|
},
|
|
),
|
|
Expr(
|
|
StmtExpr {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 112..113,
|
|
value: NumberLiteral(
|
|
ExprNumberLiteral {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 112..113,
|
|
value: Int(
|
|
1,
|
|
),
|
|
},
|
|
),
|
|
},
|
|
),
|
|
Expr(
|
|
StmtExpr {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 115..118,
|
|
value: EllipsisLiteral(
|
|
ExprEllipsisLiteral {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 115..118,
|
|
},
|
|
),
|
|
},
|
|
),
|
|
Expr(
|
|
StmtExpr {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 120..133,
|
|
value: If(
|
|
ExprIf {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 120..133,
|
|
test: Name(
|
|
ExprName {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 125..126,
|
|
id: Name("b"),
|
|
ctx: Load,
|
|
},
|
|
),
|
|
body: Name(
|
|
ExprName {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 120..121,
|
|
id: Name("a"),
|
|
ctx: Load,
|
|
},
|
|
),
|
|
orelse: Name(
|
|
ExprName {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 132..133,
|
|
id: Name("c"),
|
|
ctx: Load,
|
|
},
|
|
),
|
|
},
|
|
),
|
|
},
|
|
),
|
|
If(
|
|
StmtIf {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 135..157,
|
|
test: Name(
|
|
ExprName {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 138..139,
|
|
id: Name("c"),
|
|
ctx: Load,
|
|
},
|
|
),
|
|
body: [
|
|
Expr(
|
|
StmtExpr {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 141..142,
|
|
value: Name(
|
|
ExprName {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 141..142,
|
|
id: Name("B"),
|
|
ctx: Load,
|
|
},
|
|
),
|
|
},
|
|
),
|
|
Delete(
|
|
StmtDelete {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 144..149,
|
|
targets: [
|
|
Name(
|
|
ExprName {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 148..149,
|
|
id: Name("A"),
|
|
ctx: Del,
|
|
},
|
|
),
|
|
],
|
|
},
|
|
),
|
|
],
|
|
elif_else_clauses: [
|
|
ElifElseClause {
|
|
range: 150..157,
|
|
node_index: AtomicNodeIndex(..),
|
|
test: None,
|
|
body: [
|
|
Expr(
|
|
StmtExpr {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 156..157,
|
|
value: Name(
|
|
ExprName {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 156..157,
|
|
id: Name("C"),
|
|
ctx: Load,
|
|
},
|
|
),
|
|
},
|
|
),
|
|
],
|
|
},
|
|
],
|
|
},
|
|
),
|
|
If(
|
|
StmtIf {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 158..171,
|
|
test: Name(
|
|
ExprName {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 161..162,
|
|
id: Name("x"),
|
|
ctx: Load,
|
|
},
|
|
),
|
|
body: [
|
|
Expr(
|
|
StmtExpr {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 164..171,
|
|
value: Yield(
|
|
ExprYield {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 164..171,
|
|
value: Some(
|
|
Name(
|
|
ExprName {
|
|
node_index: AtomicNodeIndex(..),
|
|
range: 170..171,
|
|
id: Name("x"),
|
|
ctx: Load,
|
|
},
|
|
),
|
|
),
|
|
},
|
|
),
|
|
},
|
|
),
|
|
],
|
|
elif_else_clauses: [],
|
|
},
|
|
),
|
|
],
|
|
},
|
|
)
|
|
```
|