Skip to content

Commit 1518f1d

Browse files
authored
Drop excess capacity from Suites during parsing (#25368)
## Summary Shrink Suite vectors to drop the excess capacity during parsing. I excluded other nodes for now to get a better sense of the performance and memory impact. I'm a bit conflicted on this. This is a pretty huge memory improvement for ty, but there are a few linter benchmarks that regress by 1-2% because some vectors need to be copied to smaller allocations, which hurts performance. For the linter and formatter, it's also not important to shrink the vectors, because the AST is never stored for long. But this is different for ty where we cache the AST. I'm curious to hear what others think on this. I could also try to reduce the places where we call `shrink_to_fit`, e.g., only for statements?
1 parent 572e4b5 commit 1518f1d

2 files changed

Lines changed: 3 additions & 1 deletion

File tree

crates/ruff_python_parser/src/parser/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ impl<'src> Parser<'src> {
519519
) -> ThinVec<T> {
520520
let mut elements = ThinVec::new();
521521
self.parse_list(recovery_context_kind, |p| elements.push(parse_element(p)));
522+
elements.shrink_to_fit();
522523
elements
523524
}
524525

crates/ruff_python_parser/src/parser/statement.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'src> Parser<'src> {
195195
///
196196
/// [Python grammar]: https://docs.python.org/3/reference/grammar.html
197197
fn parse_simple_statements(&mut self) -> Suite {
198-
let mut stmts = Suite::new();
198+
let mut stmts = Suite::with_capacity(1);
199199
let mut progress = ParserProgress::default();
200200

201201
loop {
@@ -258,6 +258,7 @@ impl<'src> Parser<'src> {
258258

259259
// test_ok simple_stmts_with_semicolons
260260
// return; import a; from x import y; z; type T = int
261+
stmts.shrink_to_fit();
261262
stmts
262263
}
263264

0 commit comments

Comments
 (0)