From f4d17985dcb2695e083c0fcbab66b244545e481a Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Fri, 13 Dec 2024 15:17:07 +0000 Subject: [PATCH] perf(transformer/class-properties): reduce size of `transform_call_expression_for_super_member_expr` --- .../class_properties/static_prop_init.rs | 7 ++-- .../src/es2022/class_properties/supers.rs | 41 ++++++++++++++----- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/crates/oxc_transformer/src/es2022/class_properties/static_prop_init.rs b/crates/oxc_transformer/src/es2022/class_properties/static_prop_init.rs index cab38c7e479655..101cfd7110bda3 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/static_prop_init.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/static_prop_init.rs @@ -212,9 +212,10 @@ impl<'a, 'ctx, 'v> VisitMut<'a> for StaticInitializerVisitor<'a, 'ctx, 'v> { Expression::PrivateFieldExpression(_) => { self.class_properties.transform_private_field_expression(expr, self.ctx); } - // `object.#prop()` - Expression::CallExpression(_) => { - self.class_properties.transform_super_call_expression(expr, self.ctx); + // `super.prop()` or `object.#prop()` + Expression::CallExpression(call_expr) => { + self.class_properties + .transform_call_expression_for_super_member_expr(call_expr, self.ctx); self.class_properties.transform_call_expression(expr, self.ctx); } // `object.#prop = value`, `object.#prop += value`, `object.#prop ??= value` etc diff --git a/crates/oxc_transformer/src/es2022/class_properties/supers.rs b/crates/oxc_transformer/src/es2022/class_properties/supers.rs index 95be2a1ecda21d..5172260383f401 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/supers.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/supers.rs @@ -72,25 +72,46 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { /// `super.method()` -> `_superPropGet(_Class, "method", _Class, 2)([])` /// `super.method(1)` -> `_superPropGet(_Class, "method", _Class, 2)([1])` // - // `#[inline]` so that compiler sees that `expr` is an `Expression::CallExpression`. + // `#[inline]` so can bail out fast without a function call if `callee` is not a member expression + // with `super` as member expression object (fairly rare). + // Actual transform is broken out into separate functions. #[inline] - pub(super) fn transform_super_call_expression( + pub(super) fn transform_call_expression_for_super_member_expr( &mut self, - expr: &mut Expression<'a>, + call_expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>, ) { - let Expression::CallExpression(call) = expr else { unreachable!() }; - let callee = &mut call.callee; - match callee { + match &call_expr.callee { Expression::StaticMemberExpression(member) if member.object.is_super() => { - *callee = self.transform_static_member_expression_impl(member, true, ctx); + self.transform_call_expression_for_super_static_member_expr(call_expr, ctx); } Expression::ComputedMemberExpression(member) if member.object.is_super() => { - *callee = self.transform_computed_member_expression_impl(member, true, ctx); + self.transform_call_expression_for_super_computed_member_expr(call_expr, ctx); } - _ => return, + _ => {} }; - Self::transform_super_call_expression_arguments(&mut call.arguments, ctx); + } + + fn transform_call_expression_for_super_static_member_expr( + &mut self, + call_expr: &mut CallExpression<'a>, + ctx: &mut TraverseCtx<'a>, + ) { + let callee = &mut call_expr.callee; + let Expression::StaticMemberExpression(member) = callee else { unreachable!() }; + *callee = self.transform_static_member_expression_impl(member, true, ctx); + Self::transform_super_call_expression_arguments(&mut call_expr.arguments, ctx); + } + + fn transform_call_expression_for_super_computed_member_expr( + &mut self, + call_expr: &mut CallExpression<'a>, + ctx: &mut TraverseCtx<'a>, + ) { + let callee = &mut call_expr.callee; + let Expression::ComputedMemberExpression(member) = callee else { unreachable!() }; + *callee = self.transform_computed_member_expression_impl(member, true, ctx); + Self::transform_super_call_expression_arguments(&mut call_expr.arguments, ctx); } /// [A, B, C] -> [[A, B, C]]