Skip to content

Commit ea3f362

Browse files
authored
perf(codegen): reorder match arms based on usage patterns (#14496)
1 parent bc4f0a1 commit ea3f362

File tree

1 file changed

+74
-51
lines changed

1 file changed

+74
-51
lines changed

crates/oxc_codegen/src/gen.rs

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -104,39 +104,20 @@ impl Gen for Directive<'_> {
104104
impl Gen for Statement<'_> {
105105
fn r#gen(&self, p: &mut Codegen, ctx: Context) {
106106
match self {
107+
// Most common statements first (based on parser order and frequency)
107108
Self::BlockStatement(stmt) => {
108109
p.print_comments_at(stmt.span.start);
109110
stmt.print(p, ctx);
110111
}
111-
Self::BreakStatement(stmt) => stmt.print(p, ctx),
112-
Self::ContinueStatement(stmt) => stmt.print(p, ctx),
113-
Self::DebuggerStatement(stmt) => stmt.print(p, ctx),
114-
Self::DoWhileStatement(stmt) => stmt.print(p, ctx),
115-
Self::EmptyStatement(stmt) => stmt.print(p, ctx),
116112
Self::ExpressionStatement(stmt) => stmt.print(p, ctx),
117-
Self::ForInStatement(stmt) => stmt.print(p, ctx),
118-
Self::ForOfStatement(stmt) => stmt.print(p, ctx),
119-
Self::ForStatement(stmt) => stmt.print(p, ctx),
120-
Self::IfStatement(stmt) => stmt.print(p, ctx),
121-
Self::LabeledStatement(stmt) => stmt.print(p, ctx),
122-
Self::ReturnStatement(stmt) => stmt.print(p, ctx),
123-
Self::SwitchStatement(stmt) => stmt.print(p, ctx),
124-
Self::ThrowStatement(stmt) => stmt.print(p, ctx),
125-
Self::TryStatement(stmt) => stmt.print(p, ctx),
126-
Self::WhileStatement(stmt) => stmt.print(p, ctx),
127-
Self::WithStatement(stmt) => stmt.print(p, ctx),
128-
Self::ImportDeclaration(decl) => decl.print(p, ctx),
129-
Self::ExportAllDeclaration(decl) => decl.print(p, ctx),
130-
Self::ExportDefaultDeclaration(decl) => decl.print(p, ctx),
131-
Self::ExportNamedDeclaration(decl) => decl.print(p, ctx),
132-
Self::TSExportAssignment(decl) => decl.print(p, ctx),
133-
Self::TSNamespaceExportDeclaration(decl) => decl.print(p, ctx),
134113
Self::VariableDeclaration(decl) => {
135114
p.print_comments_at(decl.span.start);
136115
p.print_indent();
137116
decl.print(p, ctx);
138117
p.print_semicolon_after_statement();
139118
}
119+
Self::IfStatement(stmt) => stmt.print(p, ctx),
120+
Self::ReturnStatement(stmt) => stmt.print(p, ctx),
140121
Self::FunctionDeclaration(decl) => {
141122
p.print_comments_at(decl.span.start);
142123
if decl.pure && p.options.print_annotation_comment() {
@@ -147,12 +128,31 @@ impl Gen for Statement<'_> {
147128
decl.print(p, ctx);
148129
p.print_soft_newline();
149130
}
131+
Self::ForStatement(stmt) => stmt.print(p, ctx),
132+
Self::WhileStatement(stmt) => stmt.print(p, ctx),
133+
Self::DoWhileStatement(stmt) => stmt.print(p, ctx),
134+
Self::SwitchStatement(stmt) => stmt.print(p, ctx),
135+
Self::BreakStatement(stmt) => stmt.print(p, ctx),
136+
Self::ContinueStatement(stmt) => stmt.print(p, ctx),
137+
Self::TryStatement(stmt) => stmt.print(p, ctx),
138+
Self::ThrowStatement(stmt) => stmt.print(p, ctx),
139+
Self::ForInStatement(stmt) => stmt.print(p, ctx),
140+
Self::ForOfStatement(stmt) => stmt.print(p, ctx),
150141
Self::ClassDeclaration(decl) => {
151142
p.print_comments_at(decl.span.start);
152143
p.print_indent();
153144
decl.print(p, ctx);
154145
p.print_soft_newline();
155146
}
147+
Self::LabeledStatement(stmt) => stmt.print(p, ctx),
148+
Self::EmptyStatement(stmt) => stmt.print(p, ctx),
149+
Self::ImportDeclaration(decl) => decl.print(p, ctx),
150+
Self::ExportNamedDeclaration(decl) => decl.print(p, ctx),
151+
Self::ExportDefaultDeclaration(decl) => decl.print(p, ctx),
152+
Self::ExportAllDeclaration(decl) => decl.print(p, ctx),
153+
Self::WithStatement(stmt) => stmt.print(p, ctx),
154+
Self::DebuggerStatement(stmt) => stmt.print(p, ctx),
155+
// TypeScript-specific (less common)
156156
Self::TSModuleDeclaration(decl) => {
157157
p.print_comments_at(decl.span.start);
158158
p.print_indent();
@@ -177,6 +177,8 @@ impl Gen for Statement<'_> {
177177
decl.print(p, ctx);
178178
p.print_soft_newline();
179179
}
180+
Self::TSExportAssignment(decl) => decl.print(p, ctx),
181+
Self::TSNamespaceExportDeclaration(decl) => decl.print(p, ctx),
180182
Self::TSImportEqualsDeclaration(decl) => {
181183
p.print_indent();
182184
p.print_comments_at(decl.span.start);
@@ -1130,58 +1132,79 @@ impl Gen for ExportDefaultDeclarationKind<'_> {
11301132
impl GenExpr for Expression<'_> {
11311133
fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) {
11321134
match self {
1133-
Self::BooleanLiteral(lit) => lit.print(p, ctx),
1134-
Self::NullLiteral(lit) => lit.print(p, ctx),
1135-
Self::NumericLiteral(lit) => lit.print_expr(p, precedence, ctx),
1136-
Self::BigIntLiteral(lit) => lit.print_expr(p, precedence, ctx),
1137-
Self::RegExpLiteral(lit) => lit.print(p, ctx),
1138-
Self::StringLiteral(lit) => lit.print(p, ctx),
1135+
// Most common expressions first (identifiers, member access, calls)
11391136
Self::Identifier(ident) => ident.print(p, ctx),
1140-
Self::ThisExpression(expr) => expr.print(p, ctx),
1141-
Self::ComputedMemberExpression(expr) => expr.print_expr(p, precedence, ctx),
11421137
Self::StaticMemberExpression(expr) => expr.print_expr(p, precedence, ctx),
1143-
Self::PrivateFieldExpression(expr) => expr.print_expr(p, precedence, ctx),
1138+
Self::ComputedMemberExpression(expr) => expr.print_expr(p, precedence, ctx),
11441139
Self::CallExpression(expr) => expr.print_expr(p, precedence, ctx),
1145-
Self::ArrayExpression(expr) => expr.print(p, ctx),
1140+
// Literals (very common)
1141+
Self::NumericLiteral(lit) => lit.print_expr(p, precedence, ctx),
1142+
Self::StringLiteral(lit) => lit.print(p, ctx),
1143+
Self::BooleanLiteral(lit) => lit.print(p, ctx),
1144+
Self::NullLiteral(lit) => lit.print(p, ctx),
1145+
// Binary and logical operations (common)
1146+
Self::BinaryExpression(expr) => expr.print_expr(p, precedence, ctx),
1147+
Self::LogicalExpression(expr) => expr.print_expr(p, precedence, ctx),
1148+
// Object and array literals (common)
11461149
Self::ObjectExpression(expr) => expr.print_expr(p, precedence, ctx),
1147-
Self::FunctionExpression(func) => {
1150+
Self::ArrayExpression(expr) => expr.print(p, ctx),
1151+
// Assignment and update (common)
1152+
Self::AssignmentExpression(expr) => expr.print_expr(p, precedence, ctx),
1153+
Self::UpdateExpression(expr) => expr.print_expr(p, precedence, ctx),
1154+
Self::UnaryExpression(expr) => expr.print_expr(p, precedence, ctx),
1155+
// Conditional and sequence
1156+
Self::ConditionalExpression(expr) => expr.print_expr(p, precedence, ctx),
1157+
Self::SequenceExpression(expr) => expr.print_expr(p, precedence, ctx),
1158+
// Function expressions
1159+
Self::ArrowFunctionExpression(func) => {
11481160
if func.pure && p.options.print_annotation_comment() {
11491161
p.print_str(NO_SIDE_EFFECTS_COMMENT);
11501162
}
1151-
func.print(p, ctx);
1163+
func.print_expr(p, precedence, ctx);
11521164
}
1153-
Self::ArrowFunctionExpression(func) => {
1165+
Self::FunctionExpression(func) => {
11541166
if func.pure && p.options.print_annotation_comment() {
11551167
p.print_str(NO_SIDE_EFFECTS_COMMENT);
11561168
}
1157-
func.print_expr(p, precedence, ctx);
1169+
func.print(p, ctx);
11581170
}
1159-
Self::YieldExpression(expr) => expr.print_expr(p, precedence, ctx),
1160-
Self::UpdateExpression(expr) => expr.print_expr(p, precedence, ctx),
1161-
Self::UnaryExpression(expr) => expr.print_expr(p, precedence, ctx),
1162-
Self::BinaryExpression(expr) => expr.print_expr(p, precedence, ctx),
1163-
Self::PrivateInExpression(expr) => expr.print_expr(p, precedence, ctx),
1164-
Self::LogicalExpression(expr) => expr.print_expr(p, precedence, ctx),
1165-
Self::ConditionalExpression(expr) => expr.print_expr(p, precedence, ctx),
1166-
Self::AssignmentExpression(expr) => expr.print_expr(p, precedence, ctx),
1167-
Self::SequenceExpression(expr) => expr.print_expr(p, precedence, ctx),
1168-
Self::ImportExpression(expr) => expr.print_expr(p, precedence, ctx),
1171+
// This and super
1172+
Self::ThisExpression(expr) => expr.print(p, ctx),
1173+
Self::Super(sup) => sup.print(p, ctx),
1174+
// New expression
1175+
Self::NewExpression(expr) => expr.print_expr(p, precedence, ctx),
1176+
// Template literals
11691177
Self::TemplateLiteral(literal) => literal.print(p, ctx),
11701178
Self::TaggedTemplateExpression(expr) => expr.print(p, ctx),
1171-
Self::Super(sup) => sup.print(p, ctx),
1179+
// Other literals
1180+
Self::RegExpLiteral(lit) => lit.print(p, ctx),
1181+
Self::BigIntLiteral(lit) => lit.print_expr(p, precedence, ctx),
1182+
// Class expression
1183+
Self::ClassExpression(expr) => expr.print(p, ctx),
1184+
// Async/await and yield
11721185
Self::AwaitExpression(expr) => expr.print_expr(p, precedence, ctx),
1173-
Self::ChainExpression(expr) => expr.print_expr(p, precedence, ctx),
1174-
Self::NewExpression(expr) => expr.print_expr(p, precedence, ctx),
1186+
Self::YieldExpression(expr) => expr.print_expr(p, precedence, ctx),
1187+
// Import expression
1188+
Self::ImportExpression(expr) => expr.print_expr(p, precedence, ctx),
1189+
// Meta property
11751190
Self::MetaProperty(expr) => expr.print(p, ctx),
1176-
Self::ClassExpression(expr) => expr.print(p, ctx),
1191+
// Chain expression
1192+
Self::ChainExpression(expr) => expr.print_expr(p, precedence, ctx),
1193+
// Private field
1194+
Self::PrivateFieldExpression(expr) => expr.print_expr(p, precedence, ctx),
1195+
Self::PrivateInExpression(expr) => expr.print_expr(p, precedence, ctx),
1196+
// Parenthesized
1197+
Self::ParenthesizedExpression(e) => e.print_expr(p, precedence, ctx),
1198+
// JSX (less common in typical JS code)
11771199
Self::JSXElement(el) => el.print(p, ctx),
11781200
Self::JSXFragment(fragment) => fragment.print(p, ctx),
1179-
Self::ParenthesizedExpression(e) => e.print_expr(p, precedence, ctx),
1201+
// TypeScript (less common in runtime)
11801202
Self::TSAsExpression(e) => e.print_expr(p, precedence, ctx),
11811203
Self::TSSatisfiesExpression(e) => e.print_expr(p, precedence, ctx),
11821204
Self::TSTypeAssertion(e) => e.print_expr(p, precedence, ctx),
11831205
Self::TSNonNullExpression(e) => e.print_expr(p, precedence, ctx),
11841206
Self::TSInstantiationExpression(e) => e.print_expr(p, precedence, ctx),
1207+
// V8 intrinsics (rare)
11851208
Self::V8IntrinsicExpression(e) => e.print_expr(p, precedence, ctx),
11861209
}
11871210
}

0 commit comments

Comments
 (0)