Skip to content

Commit d503a84

Browse files
committed
perf(parser): reorder parse_statement match conditions (#7645)
For performance reasons, match orders are: 1. plain if check 2. check current token 3. peek token
1 parent e923e4e commit d503a84

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

crates/oxc_parser/src/js/expression.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ impl<'a> ParserImpl<'a> {
170170

171171
match self.cur_kind() {
172172
Kind::Ident => self.parse_identifier_expression(), // fast path, keywords are checked at the end
173-
// Literal, RegularExpressionLiteral
174-
kind if kind.is_literal() => self.parse_literal_expression(),
175173
// ArrayLiteral
176174
Kind::LBrack => self.parse_array_expression(),
177175
// ObjectLiteral
@@ -199,6 +197,8 @@ impl<'a> ParserImpl<'a> {
199197
Kind::Slash | Kind::SlashEq => self
200198
.parse_literal_regexp()
201199
.map(|literal| Expression::RegExpLiteral(self.alloc(literal))),
200+
// Literal, RegularExpressionLiteral
201+
kind if kind.is_literal() => self.parse_literal_expression(),
202202
// JSXElement, JSXFragment
203203
Kind::LAngle if self.source_type.is_jsx() => self.parse_jsx_expression(),
204204
_ => self.parse_identifier_expression(),

crates/oxc_parser/src/js/statement.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ impl<'a> ParserImpl<'a> {
8787
self.eat_decorators()?;
8888
}
8989

90+
// For performance reasons, match orders are:
91+
// 1. plain if check
92+
// 2. check current token
93+
// 3. peek token
9094
match self.cur_kind() {
9195
Kind::LCurly => self.parse_block_statement(),
9296
Kind::Semicolon => Ok(self.parse_empty_statement()),
@@ -101,24 +105,28 @@ impl<'a> ParserImpl<'a> {
101105
Kind::Try => self.parse_try_statement(),
102106
Kind::Debugger => self.parse_debugger_statement(),
103107
Kind::Class => self.parse_class_statement(stmt_ctx, start_span),
104-
Kind::Import if !matches!(self.peek_kind(), Kind::Dot | Kind::LParen) => {
105-
self.parse_import_declaration()
106-
}
107108
Kind::Export => self.parse_export_declaration(),
108109
// [+Return] ReturnStatement[?Yield, ?Await]
109110
Kind::Return => self.parse_return_statement(),
110111
Kind::Var => self.parse_variable_statement(stmt_ctx),
112+
// Fast path
113+
Kind::Function => self.parse_function_declaration(stmt_ctx),
114+
Kind::Let if !self.cur_token().escaped() => self.parse_let(stmt_ctx),
115+
Kind::Import if !matches!(self.peek_kind(), Kind::Dot | Kind::LParen) => {
116+
self.parse_import_declaration()
117+
}
111118
Kind::Const if !(self.is_ts && self.is_at_enum_declaration()) => {
112119
self.parse_variable_statement(stmt_ctx)
113120
}
114-
Kind::Let if !self.cur_token().escaped() => self.parse_let(stmt_ctx),
115121
Kind::Await
116122
if self.peek_kind() == Kind::Using && self.nth_kind(2).is_binding_identifier() =>
117123
{
118124
self.parse_using()
119125
}
120126
Kind::Using if self.peek_kind().is_binding_identifier() => self.parse_using(),
121-
_ if self.at_function_with_async() => self.parse_function_declaration(stmt_ctx),
127+
Kind::Async if self.peek_at(Kind::Function) && !self.peek_token().is_on_new_line => {
128+
self.parse_function_declaration(stmt_ctx)
129+
}
122130
_ if self.is_ts && self.at_start_of_ts_declaration() => {
123131
self.parse_ts_declaration_statement(start_span)
124132
}

0 commit comments

Comments
 (0)