Skip to content

Commit 4f61c24

Browse files
committed
Avoid allocating vecs for each statement
1 parent 851f236 commit 4f61c24

File tree

2 files changed

+9148
-8887
lines changed

2 files changed

+9148
-8887
lines changed

parser/src/python.lalrpop

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,42 @@ pub Top: ast::Mod = {
2626
};
2727

2828
Program: ast::Suite = {
29-
<lines:FileLine*> => {
30-
lines.into_iter().flatten().collect()
31-
},
32-
};
29+
=> vec![],
30+
Statements,
3331

34-
// A file line either has a declaration, or an empty newline:
35-
FileLine: ast::Suite = {
36-
Statement,
37-
"\n" => vec![],
3832
};
3933

4034
Suite: ast::Suite = {
41-
SimpleStatement,
42-
"\n" Indent <s:Statement+> Dedent => s.into_iter().flatten().collect(),
35+
<mut statements:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
36+
statements.push(last);
37+
statements
38+
},
39+
"\n" Indent <s:Statements> Dedent => s,
4340
};
4441

45-
Statement: ast::Suite = {
46-
SimpleStatement,
42+
43+
Statements: Vec<ast::Stmt> = {
44+
// First simple statement
45+
<mut head:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
46+
head.push(last);
47+
head
48+
},
49+
50+
// The first compound statement
4751
<s:CompoundStatement> => vec![s],
48-
};
4952

50-
SimpleStatement: ast::Suite = {
51-
<mut statements:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
53+
// Any subsequent compound statements
54+
<mut statements:Statements> <next:CompoundStatement> => {
55+
statements.push(next);
56+
statements
57+
},
58+
59+
// Any subsequent small statements
60+
<mut statements:Statements> <small:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
61+
statements.extend(small);
5262
statements.push(last);
5363
statements
54-
}
64+
},
5565
};
5666

5767
SmallStatement: ast::Stmt = {

0 commit comments

Comments
 (0)