Files
ruff/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__compare.py.snap
Charlie Marsh 7b13f01ba1 [ty] Reduce retained AST memory by shrinking expressions (#28335)
## Summary

We reduce `Expr` from 64 to 56 bytes on 64-bit targets. The largest
variants determine the enum's size, so shrinking those variants saves
eight bytes in every `Expr` value, including expressions in the parsed
modules retained by ty.

The changes are:

- Store positional call arguments in `ThinVec`, reducing `Arguments` and
`ExprCall` by eight bytes.
- Store all comparison operands in one boxed slice. This removes the
separate left-operand allocation and reduces `ExprCompare` by eight
bytes while keeping operators in a boxed slice.
- Store dictionary-comprehension generators in a boxed slice, reducing
`ExprDictComp` by eight bytes.
- Store f-string and t-string elements in boxed slices, and represent a
single f-string directly as `FString`. This keeps single parts inline
and reduces `ExprFString` and `ExprTString` by eight bytes.

The parser, AST visitors, formatter, lint rules, and type inference use
the updated layouts. Size assertions cover the 56-byte enum and its
affected variants.
2026-09-10 15:39:14 -04:00

793 lines
34 KiB
Plaintext

---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/valid/expressions/compare.py
---
## AST
```
Module(
ModModule {
node_index: NodeIndex(None),
range: 0..542,
body: [
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 9..15,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 9..15,
ops: [
Eq,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 9..10,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 14..15,
id: Name("b"),
ctx: Load,
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 16..21,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 16..21,
ops: [
Lt,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 16..17,
id: Name("b"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 20..21,
id: Name("a"),
ctx: Load,
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 22..27,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 22..27,
ops: [
Gt,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 22..23,
id: Name("b"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 26..27,
id: Name("a"),
ctx: Load,
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 28..34,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 28..34,
ops: [
GtE,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 28..29,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 33..34,
id: Name("b"),
ctx: Load,
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 35..41,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 35..41,
ops: [
LtE,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 35..36,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 40..41,
id: Name("b"),
ctx: Load,
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 42..48,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 42..48,
ops: [
NotEq,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 42..43,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 47..48,
id: Name("b"),
ctx: Load,
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 49..55,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 49..55,
ops: [
Is,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 49..50,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 54..55,
id: Name("c"),
ctx: Load,
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 56..62,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 56..62,
ops: [
In,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 56..57,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 61..62,
id: Name("b"),
ctx: Load,
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 63..73,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 63..73,
ops: [
NotIn,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 63..64,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 72..73,
id: Name("c"),
ctx: Load,
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 74..84,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 74..84,
ops: [
IsNot,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 74..75,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 83..84,
id: Name("b"),
ctx: Load,
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 110..156,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 110..156,
ops: [
NotIn,
IsNot,
NotIn,
NotIn,
IsNot,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 110..111,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 119..120,
id: Name("b"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 128..129,
id: Name("c"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 137..138,
id: Name("d"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 146..147,
id: Name("e"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 155..156,
id: Name("f"),
ctx: Load,
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 177..203,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 177..203,
ops: [
Lt,
NotIn,
],
operands: [
BinOp(
ExprBinOp {
node_index: NodeIndex(None),
range: 177..182,
left: Name(
ExprName {
node_index: NodeIndex(None),
range: 177..178,
id: Name("a"),
ctx: Load,
},
),
op: BitOr,
right: Name(
ExprName {
node_index: NodeIndex(None),
range: 181..182,
id: Name("b"),
ctx: Load,
},
),
},
),
BinOp(
ExprBinOp {
node_index: NodeIndex(None),
range: 185..190,
left: Name(
ExprName {
node_index: NodeIndex(None),
range: 185..186,
id: Name("c"),
ctx: Load,
},
),
op: BitOr,
right: Name(
ExprName {
node_index: NodeIndex(None),
range: 189..190,
id: Name("d"),
ctx: Load,
},
),
},
),
BinOp(
ExprBinOp {
node_index: NodeIndex(None),
range: 198..203,
left: Name(
ExprName {
node_index: NodeIndex(None),
range: 198..199,
id: Name("e"),
ctx: Load,
},
),
op: BitAnd,
right: Name(
ExprName {
node_index: NodeIndex(None),
range: 202..203,
id: Name("f"),
ctx: Load,
},
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 379..393,
value: UnaryOp(
ExprUnaryOp {
node_index: NodeIndex(None),
range: 379..393,
op: Not,
operand: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 383..393,
ops: [
NotIn,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 383..384,
id: Name("x"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 392..393,
id: Name("y"),
ctx: Load,
},
),
],
},
),
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 395..416,
value: BoolOp(
ExprBoolOp {
node_index: NodeIndex(None),
range: 395..416,
op: Or,
values: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 395..396,
id: Name("x"),
ctx: Load,
},
),
BoolOp(
ExprBoolOp {
node_index: NodeIndex(None),
range: 400..416,
op: And,
values: [
Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 400..410,
ops: [
NotIn,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 400..401,
id: Name("y"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 409..410,
id: Name("z"),
ctx: Load,
},
),
],
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 415..416,
id: Name("a"),
ctx: Load,
},
),
],
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 417..429,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 417..429,
ops: [
Eq,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 417..418,
id: Name("x"),
ctx: Load,
},
),
Await(
ExprAwait {
node_index: NodeIndex(None),
range: 422..429,
value: Name(
ExprName {
node_index: NodeIndex(None),
range: 428..429,
id: Name("y"),
ctx: Load,
},
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 430..446,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 430..446,
ops: [
IsNot,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 430..431,
id: Name("x"),
ctx: Load,
},
),
Await(
ExprAwait {
node_index: NodeIndex(None),
range: 439..446,
value: Name(
ExprName {
node_index: NodeIndex(None),
range: 445..446,
id: Name("y"),
ctx: Load,
},
),
},
),
],
},
),
},
),
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 489..541,
value: Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 489..541,
ops: [
Lt,
Eq,
Gt,
Is,
NotIn,
IsNot,
LtE,
GtE,
NotEq,
],
operands: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 489..490,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 493..494,
id: Name("b"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 498..499,
id: Name("c"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 502..503,
id: Name("d"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 507..508,
id: Name("e"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 516..517,
id: Name("f"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 525..526,
id: Name("g"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 530..531,
id: Name("h"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 535..536,
id: Name("i"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 540..541,
id: Name("j"),
ctx: Load,
},
),
],
},
),
},
),
],
},
)
```