ruff/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__set.py.snap
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

708 lines
32 KiB
Text

---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/valid/expressions/set.py
---
## AST
```
Module(
ModModule {
node_index: AtomicNodeIndex(..),
range: 0..313,
body: [
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 14..16,
value: Dict(
ExprDict {
node_index: AtomicNodeIndex(..),
range: 14..16,
items: [],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 17..20,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 17..20,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 18..19,
value: Int(
1,
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 21..25,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 21..25,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 22..23,
value: Int(
1,
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 26..35,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 26..35,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 27..28,
value: Int(
1,
),
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 30..31,
value: Int(
2,
),
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 33..34,
value: Int(
3,
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 36..46,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 36..46,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 37..38,
value: Int(
1,
),
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 40..41,
value: Int(
2,
),
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 43..44,
value: Int(
3,
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 74..77,
value: Dict(
ExprDict {
node_index: AtomicNodeIndex(..),
range: 74..77,
items: [],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 78..91,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 78..91,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 88..89,
value: Int(
1,
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 92..113,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 92..113,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 98..99,
value: Int(
1,
),
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 109..110,
value: Int(
2,
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 124..129,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 124..129,
elts: [
Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 125..128,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 126..127,
value: Int(
1,
),
},
),
],
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 130..146,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 130..146,
elts: [
Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 131..137,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 132..133,
value: Int(
1,
),
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 135..136,
value: Int(
2,
),
},
),
],
},
),
Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 139..145,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 140..141,
value: Int(
3,
),
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 143..144,
value: Int(
4,
),
},
),
],
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 167..175,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 167..175,
elts: [
Named(
ExprNamed {
node_index: AtomicNodeIndex(..),
range: 168..174,
target: Name(
ExprName {
node_index: AtomicNodeIndex(..),
range: 168..169,
id: Name("x"),
ctx: Store,
},
),
value: NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 173..174,
value: Int(
2,
),
},
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 176..190,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 176..190,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 177..178,
value: Int(
1,
),
},
),
Named(
ExprNamed {
node_index: AtomicNodeIndex(..),
range: 180..186,
target: Name(
ExprName {
node_index: AtomicNodeIndex(..),
range: 180..181,
id: Name("x"),
ctx: Store,
},
),
value: NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 185..186,
value: Int(
2,
),
},
),
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 188..189,
value: Int(
3,
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 191..205,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 191..205,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 192..193,
value: Int(
1,
),
},
),
Named(
ExprNamed {
node_index: AtomicNodeIndex(..),
range: 196..202,
target: Name(
ExprName {
node_index: AtomicNodeIndex(..),
range: 196..197,
id: Name("x"),
ctx: Store,
},
),
value: NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 201..202,
value: Int(
2,
),
},
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 225..235,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 225..235,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 226..227,
value: Int(
1,
),
},
),
Starred(
ExprStarred {
node_index: AtomicNodeIndex(..),
range: 229..231,
value: Name(
ExprName {
node_index: AtomicNodeIndex(..),
range: 230..231,
id: Name("x"),
ctx: Load,
},
),
ctx: Load,
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 233..234,
value: Int(
3,
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 236..250,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 236..250,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 237..238,
value: Int(
1,
),
},
),
Starred(
ExprStarred {
node_index: AtomicNodeIndex(..),
range: 240..246,
value: BinOp(
ExprBinOp {
node_index: AtomicNodeIndex(..),
range: 241..246,
left: Name(
ExprName {
node_index: AtomicNodeIndex(..),
range: 241..242,
id: Name("x"),
ctx: Load,
},
),
op: BitOr,
right: Name(
ExprName {
node_index: AtomicNodeIndex(..),
range: 245..246,
id: Name("y"),
ctx: Load,
},
),
},
),
ctx: Load,
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 248..249,
value: Int(
3,
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: AtomicNodeIndex(..),
range: 273..312,
value: Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 273..312,
elts: [
BinOp(
ExprBinOp {
node_index: AtomicNodeIndex(..),
range: 274..279,
left: NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 274..275,
value: Int(
1,
),
},
),
op: Add,
right: NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 278..279,
value: Int(
2,
),
},
),
},
),
Tuple(
ExprTuple {
node_index: AtomicNodeIndex(..),
range: 281..287,
elts: [
Name(
ExprName {
node_index: AtomicNodeIndex(..),
range: 282..283,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: AtomicNodeIndex(..),
range: 285..286,
id: Name("b"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: true,
},
),
Set(
ExprSet {
node_index: AtomicNodeIndex(..),
range: 289..298,
elts: [
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 290..291,
value: Int(
1,
),
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 293..294,
value: Int(
2,
),
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: AtomicNodeIndex(..),
range: 296..297,
value: Int(
3,
),
},
),
],
},
),
Dict(
ExprDict {
node_index: AtomicNodeIndex(..),
range: 300..311,
items: [
DictItem {
key: Some(
Name(
ExprName {
node_index: AtomicNodeIndex(..),
range: 301..302,
id: Name("a"),
ctx: Load,
},
),
),
value: Name(
ExprName {
node_index: AtomicNodeIndex(..),
range: 304..305,
id: Name("b"),
ctx: Load,
},
),
},
DictItem {
key: None,
value: Name(
ExprName {
node_index: AtomicNodeIndex(..),
range: 309..310,
id: Name("d"),
ctx: Load,
},
),
},
],
},
),
],
},
),
},
),
],
},
)
```