From d18c78c0dba6bc481fb8ee116a4da9e4febaeee6 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Tue, 7 Jan 2025 13:50:30 +0000 Subject: [PATCH] perf(transformer/arrow-functions): reduce size of inlined visitor --- .../src/common/arrow_function_converter.rs | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/crates/oxc_transformer/src/common/arrow_function_converter.rs b/crates/oxc_transformer/src/common/arrow_function_converter.rs index 41a466e5f8831f..8b8ba6c2da1e45 100644 --- a/crates/oxc_transformer/src/common/arrow_function_converter.rs +++ b/crates/oxc_transformer/src/common/arrow_function_converter.rs @@ -1128,7 +1128,7 @@ impl<'a, 'v> ConstructorBodyThisAfterSuperInserter<'a, 'v> { } impl<'a> VisitMut<'a> for ConstructorBodyThisAfterSuperInserter<'a, '_> { - #[inline] + #[inline] // `#[inline]` because is a no-op fn visit_class(&mut self, _class: &mut Class<'a>) { // Do not need to insert in nested classes } @@ -1142,7 +1142,9 @@ impl<'a> VisitMut<'a> for ConstructorBodyThisAfterSuperInserter<'a, '_> { self.visit_statement(stmt); continue; }; - if let Some(assignment) = self.transform_super_call_expression(&expr_stmt.expression) { + + if expr_stmt.expression.is_super_call_expression() { + let assignment = self.create_assignment_to_this_temp_var(); let new_stmt = self.ctx.ast.statement_expression(SPAN, assignment); new_stmts.push((index, new_stmt)); } else { @@ -1157,13 +1159,11 @@ impl<'a> VisitMut<'a> for ConstructorBodyThisAfterSuperInserter<'a, '_> { } /// `const A = super()` -> `const A = (super(), _this = this);` + // `#[inline]` to avoid a function call for all `Expressions` which are not `super()` (vast majority) #[inline] fn visit_expression(&mut self, expr: &mut Expression<'a>) { - if let Some(assignment) = self.transform_super_call_expression(expr) { - let span = expr.span(); - let exprs = - self.ctx.ast.vec_from_array([self.ctx.ast.move_expression(expr), assignment]); - *expr = self.ctx.ast.expression_sequence(span, exprs); + if expr.is_super_call_expression() { + self.transform_super_call_expression(expr); } else { walk_expression(self, expr); } @@ -1171,18 +1171,21 @@ impl<'a> VisitMut<'a> for ConstructorBodyThisAfterSuperInserter<'a, '_> { } impl<'a> ConstructorBodyThisAfterSuperInserter<'a, '_> { - #[inline] - fn transform_super_call_expression(&mut self, expr: &Expression<'a>) -> Option> { - if expr.is_super_call_expression() { - let assignment = self.ctx.ast.expression_assignment( - SPAN, - AssignmentOperator::Assign, - self.this_var_binding.create_write_target(self.ctx), - self.ctx.ast.expression_this(SPAN), - ); - Some(assignment) - } else { - None - } + /// `super()` -> `(super(), _this = this)` + fn transform_super_call_expression(&mut self, expr: &mut Expression<'a>) { + let assignment = self.create_assignment_to_this_temp_var(); + let span = expr.span(); + let exprs = self.ctx.ast.vec_from_array([self.ctx.ast.move_expression(expr), assignment]); + *expr = self.ctx.ast.expression_sequence(span, exprs); + } + + /// `_this = this` + fn create_assignment_to_this_temp_var(&mut self) -> Expression<'a> { + self.ctx.ast.expression_assignment( + SPAN, + AssignmentOperator::Assign, + self.this_var_binding.create_write_target(self.ctx), + self.ctx.ast.expression_this(SPAN), + ) } }