Skip to content

Commit 10ccd51

Browse files
authored
Shrink additional parser AST collections (#25465)
1 parent 0d7135f commit 10ccd51

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

crates/ruff_python_parser/src/parser/expression.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,8 @@ impl<'src> Parser<'src> {
10041004
slices.push(parser.parse_slice());
10051005
});
10061006

1007+
slices.shrink_to_fit();
1008+
10071009
slice = Expr::Tuple(ast::ExprTuple {
10081010
elts: slices,
10091011
ctx: ExprContext::Load,
@@ -1260,6 +1262,8 @@ impl<'src> Parser<'src> {
12601262
}
12611263
}
12621264

1265+
values.shrink_to_fit();
1266+
12631267
ast::ExprBoolOp {
12641268
values,
12651269
op,
@@ -2476,6 +2480,8 @@ impl<'src> Parser<'src> {
24762480
self.expect(TokenKind::Rpar);
24772481
}
24782482

2483+
elts.shrink_to_fit();
2484+
24792485
ast::ExprTuple {
24802486
elts,
24812487
ctx: ExprContext::Load,
@@ -2505,6 +2511,8 @@ impl<'src> Parser<'src> {
25052511

25062512
self.expect(TokenKind::Rsqb);
25072513

2514+
elts.shrink_to_fit();
2515+
25082516
ast::ExprList {
25092517
elts,
25102518
ctx: ExprContext::Load,
@@ -2599,6 +2607,8 @@ impl<'src> Parser<'src> {
25992607

26002608
self.expect(TokenKind::Rbrace);
26012609

2610+
items.shrink_to_fit();
2611+
26022612
ast::ExprDict {
26032613
range: self.node_range(start),
26042614
node_index: AtomicNodeIndex::NONE,
@@ -2623,6 +2633,8 @@ impl<'src> Parser<'src> {
26232633
generators.push(self.parse_comprehension());
26242634
}
26252635

2636+
generators.shrink_to_fit();
2637+
26262638
generators
26272639
}
26282640

@@ -2667,6 +2679,8 @@ impl<'src> Parser<'src> {
26672679
ifs.push(parsed_expr.expr);
26682680
}
26692681

2682+
ifs.shrink_to_fit();
2683+
26702684
ast::Comprehension {
26712685
range: self.node_range(start),
26722686
node_index: AtomicNodeIndex::NONE,

crates/ruff_python_parser/src/parser/recovery.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ pub(super) fn pattern_to_expr(pattern: Pattern) -> Expr {
8080
});
8181
items.push(ast::DictItem { key: None, value });
8282
}
83+
items.shrink_to_fit();
84+
8385
Expr::Dict(ast::ExprDict {
8486
range,
8587
node_index,

crates/ruff_python_parser/src/parser/statement.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ impl<'src> Parser<'src> {
614614
// import ,
615615
// import x, y,
616616

617-
let names = self
617+
let mut names = self
618618
.parse_comma_separated_list_into_vec(RecoveryContextKind::ImportNames, |p| {
619619
p.parse_alias(ImportStyle::Import)
620620
});
@@ -625,6 +625,8 @@ impl<'src> Parser<'src> {
625625
self.add_error(ParseErrorType::EmptyImportNames, self.current_token_range());
626626
}
627627

628+
names.shrink_to_fit();
629+
628630
ast::StmtImport {
629631
names,
630632
is_lazy,
@@ -740,6 +742,8 @@ impl<'src> Parser<'src> {
740742
self.expect(TokenKind::Rpar);
741743
}
742744

745+
names.shrink_to_fit();
746+
743747
ast::StmtImportFrom {
744748
module,
745749
names,
@@ -1444,6 +1448,8 @@ impl<'src> Parser<'src> {
14441448
elif_else_clauses.push(self.parse_elif_or_else_clause(ElifOrElse::Else));
14451449
}
14461450

1451+
elif_else_clauses.shrink_to_fit();
1452+
14471453
ast::StmtIf {
14481454
test: Box::new(test.expr),
14491455
body,
@@ -1539,7 +1545,7 @@ impl<'src> Parser<'src> {
15391545
// except* ExceptionGroup:
15401546
// pass
15411547
let mut mixed_except_ranges = Vec::new();
1542-
let handlers = self.parse_clauses(Clause::Except, |p| {
1548+
let mut handlers = self.parse_clauses(Clause::Except, |p| {
15431549
let (handler, kind) = p.parse_except_clause();
15441550
if let ExceptClauseKind::Star(range) = kind {
15451551
p.add_unsupported_syntax_error(UnsupportedSyntaxErrorKind::ExceptStar, range);
@@ -1551,6 +1557,8 @@ impl<'src> Parser<'src> {
15511557
}
15521558
handler
15531559
});
1560+
handlers.shrink_to_fit();
1561+
15541562
// Empty handler has `is_star` false.
15551563
let is_star = is_star.unwrap_or_default();
15561564
for handler_err_range in mixed_except_ranges {
@@ -2170,7 +2178,9 @@ impl<'src> Parser<'src> {
21702178
fn parse_with_statement(&mut self, start: TextSize) -> ast::StmtWith {
21712179
self.bump(TokenKind::With);
21722180

2173-
let items = self.parse_with_items();
2181+
let mut items = self.parse_with_items();
2182+
items.shrink_to_fit();
2183+
21742184
self.expect(TokenKind::Colon);
21752185

21762186
let body = self.parse_body(Clause::With);
@@ -2985,6 +2995,8 @@ impl<'src> Parser<'src> {
29852995
self.expect(TokenKind::Newline);
29862996
}
29872997

2998+
decorators.shrink_to_fit();
2999+
29883000
match self.current_token_kind() {
29893001
TokenKind::Def => Stmt::FunctionDef(self.parse_function_definition(decorators, start)),
29903002
TokenKind::Class => Stmt::ClassDef(self.parse_class_definition(decorators, start)),
@@ -3528,6 +3540,10 @@ impl<'src> Parser<'src> {
35283540
self.expect(TokenKind::Rpar);
35293541
}
35303542

3543+
parameters.args.shrink_to_fit();
3544+
parameters.kwonlyargs.shrink_to_fit();
3545+
parameters.posonlyargs.shrink_to_fit();
3546+
35313547
parameters.range = self.node_range(start);
35323548

35333549
parameters

0 commit comments

Comments
 (0)