Skip to content

perf(oxc_ast): reduce ast memory usage #5645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ pub struct PrivateFieldExpression<'a> {
pub struct CallExpression<'a> {
#[serde(flatten)]
pub span: Span,
pub callee: Expression<'a>,
pub callee: Box<'a, Expression<'a>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't make sense to me. It introduces indirection to save 8 bytes. And it's not really saving any bytes anyway as the Expression is still stored in arena, just in a separate allocation - in fact overall it costs an extra 8 bytes for the additional pointer.

pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
pub arguments: Vec<'a, Argument<'a>>,
pub optional: bool, // for optional chaining
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub struct JSXOpeningElement<'a> {
/// <Foo> // <- self_closing = false
/// ```
pub self_closing: bool,
pub name: JSXElementName<'a>,
pub name: Box<'a, JSXElementName<'a>>,
/// List of JSX attributes. In React-like applications, these become props.
pub attributes: Vec<'a, JSXAttributeItem<'a>>,
/// Type parameters for generic JSX elements.
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl<'a> Expression<'a> {
}

pub fn is_super_call_expression(&self) -> bool {
matches!(self, Expression::CallExpression(expr) if matches!(&expr.callee, Expression::Super(_)))
matches!(self, Expression::CallExpression(expr) if matches!(&*expr.callee, Expression::Super(_)))
}

pub fn is_call_like_expression(&self) -> bool {
Expand Down Expand Up @@ -560,7 +560,7 @@ impl<'a> StaticMemberExpression<'a> {

impl<'a> CallExpression<'a> {
pub fn callee_name(&self) -> Option<&str> {
match &self.callee {
match &*self.callee {
Expression::Identifier(ident) => Some(ident.name.as_str()),
expr => expr.as_member_expression().and_then(MemberExpression::static_property_name),
}
Expand All @@ -570,7 +570,7 @@ impl<'a> CallExpression<'a> {
if self.arguments.len() != 1 {
return false;
}
if let Expression::Identifier(id) = &self.callee {
if let Expression::Identifier(id) = &*self.callee {
id.name == "require"
&& matches!(
self.arguments.first(),
Expand All @@ -583,7 +583,7 @@ impl<'a> CallExpression<'a> {

pub fn is_symbol_or_symbol_for_call(&self) -> bool {
// TODO: is 'Symbol' reference to global object
match &self.callee {
match &*self.callee {
Expression::Identifier(id) => id.name == "Symbol",
expr => match expr.as_member_expression() {
Some(member) => {
Expand Down
28 changes: 14 additions & 14 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ const _: () = {
assert!(offset_of!(PrivateFieldExpression, field) == 24usize);
assert!(offset_of!(PrivateFieldExpression, optional) == 48usize);

assert!(size_of::<CallExpression>() == 72usize);
assert!(size_of::<CallExpression>() == 64usize);
assert!(align_of::<CallExpression>() == 8usize);
assert!(offset_of!(CallExpression, span) == 0usize);
assert!(offset_of!(CallExpression, callee) == 8usize);
assert!(offset_of!(CallExpression, type_parameters) == 24usize);
assert!(offset_of!(CallExpression, arguments) == 32usize);
assert!(offset_of!(CallExpression, optional) == 64usize);
assert!(offset_of!(CallExpression, type_parameters) == 16usize);
assert!(offset_of!(CallExpression, arguments) == 24usize);
assert!(offset_of!(CallExpression, optional) == 56usize);

assert!(size_of::<NewExpression>() == 64usize);
assert!(align_of::<NewExpression>() == 8usize);
Expand Down Expand Up @@ -1278,13 +1278,13 @@ const _: () = {
assert!(offset_of!(JSXElement, closing_element) == 16usize);
assert!(offset_of!(JSXElement, children) == 24usize);

assert!(size_of::<JSXOpeningElement>() == 72usize);
assert!(size_of::<JSXOpeningElement>() == 64usize);
assert!(align_of::<JSXOpeningElement>() == 8usize);
assert!(offset_of!(JSXOpeningElement, span) == 0usize);
assert!(offset_of!(JSXOpeningElement, self_closing) == 8usize);
assert!(offset_of!(JSXOpeningElement, name) == 16usize);
assert!(offset_of!(JSXOpeningElement, attributes) == 32usize);
assert!(offset_of!(JSXOpeningElement, type_parameters) == 64usize);
assert!(offset_of!(JSXOpeningElement, attributes) == 24usize);
assert!(offset_of!(JSXOpeningElement, type_parameters) == 56usize);

assert!(size_of::<JSXClosingElement>() == 24usize);
assert!(align_of::<JSXClosingElement>() == 8usize);
Expand Down Expand Up @@ -1734,13 +1734,13 @@ const _: () = {
assert!(offset_of!(PrivateFieldExpression, field) == 16usize);
assert!(offset_of!(PrivateFieldExpression, optional) == 32usize);

assert!(size_of::<CallExpression>() == 40usize);
assert!(size_of::<CallExpression>() == 36usize);
assert!(align_of::<CallExpression>() == 4usize);
assert!(offset_of!(CallExpression, span) == 0usize);
assert!(offset_of!(CallExpression, callee) == 8usize);
assert!(offset_of!(CallExpression, type_parameters) == 16usize);
assert!(offset_of!(CallExpression, arguments) == 20usize);
assert!(offset_of!(CallExpression, optional) == 36usize);
assert!(offset_of!(CallExpression, type_parameters) == 12usize);
assert!(offset_of!(CallExpression, arguments) == 16usize);
assert!(offset_of!(CallExpression, optional) == 32usize);

assert!(size_of::<NewExpression>() == 36usize);
assert!(align_of::<NewExpression>() == 4usize);
Expand Down Expand Up @@ -2832,13 +2832,13 @@ const _: () = {
assert!(offset_of!(JSXElement, closing_element) == 12usize);
assert!(offset_of!(JSXElement, children) == 16usize);

assert!(size_of::<JSXOpeningElement>() == 40usize);
assert!(size_of::<JSXOpeningElement>() == 36usize);
assert!(align_of::<JSXOpeningElement>() == 4usize);
assert!(offset_of!(JSXOpeningElement, span) == 0usize);
assert!(offset_of!(JSXOpeningElement, self_closing) == 8usize);
assert!(offset_of!(JSXOpeningElement, name) == 12usize);
assert!(offset_of!(JSXOpeningElement, attributes) == 20usize);
assert!(offset_of!(JSXOpeningElement, type_parameters) == 36usize);
assert!(offset_of!(JSXOpeningElement, attributes) == 16usize);
assert!(offset_of!(JSXOpeningElement, type_parameters) == 32usize);

assert!(size_of::<JSXClosingElement>() == 16usize);
assert!(align_of::<JSXClosingElement>() == 4usize);
Expand Down
58 changes: 32 additions & 26 deletions crates/oxc_ast/src/generated/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,16 +684,17 @@ impl<'a> AstBuilder<'a> {
/// - arguments
/// - optional
#[inline]
pub fn expression_call<T1>(
pub fn expression_call<T1, T2>(
self,
span: Span,
callee: Expression<'a>,
type_parameters: T1,
callee: T1,
type_parameters: T2,
arguments: Vec<'a, Argument<'a>>,
optional: bool,
) -> Expression<'a>
where
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
T1: IntoIn<'a, Box<'a, Expression<'a>>>,
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
{
Expression::CallExpression(self.alloc(self.call_expression(
span,
Expand Down Expand Up @@ -2299,20 +2300,21 @@ impl<'a> AstBuilder<'a> {
/// - arguments
/// - optional
#[inline]
pub fn call_expression<T1>(
pub fn call_expression<T1, T2>(
self,
span: Span,
callee: Expression<'a>,
type_parameters: T1,
callee: T1,
type_parameters: T2,
arguments: Vec<'a, Argument<'a>>,
optional: bool,
) -> CallExpression<'a>
where
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
T1: IntoIn<'a, Box<'a, Expression<'a>>>,
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
{
CallExpression {
span,
callee,
callee: callee.into_in(self.allocator),
type_parameters: type_parameters.into_in(self.allocator),
arguments,
optional,
Expand All @@ -2330,16 +2332,17 @@ impl<'a> AstBuilder<'a> {
/// - arguments
/// - optional
#[inline]
pub fn alloc_call_expression<T1>(
pub fn alloc_call_expression<T1, T2>(
self,
span: Span,
callee: Expression<'a>,
type_parameters: T1,
callee: T1,
type_parameters: T2,
arguments: Vec<'a, Argument<'a>>,
optional: bool,
) -> Box<'a, CallExpression<'a>>
where
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
T1: IntoIn<'a, Box<'a, Expression<'a>>>,
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
{
Box::new_in(
self.call_expression(span, callee, type_parameters, arguments, optional),
Expand Down Expand Up @@ -3501,16 +3504,17 @@ impl<'a> AstBuilder<'a> {
/// - arguments
/// - optional
#[inline]
pub fn chain_element_call_expression<T1>(
pub fn chain_element_call_expression<T1, T2>(
self,
span: Span,
callee: Expression<'a>,
type_parameters: T1,
callee: T1,
type_parameters: T2,
arguments: Vec<'a, Argument<'a>>,
optional: bool,
) -> ChainElement<'a>
where
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
T1: IntoIn<'a, Box<'a, Expression<'a>>>,
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
{
ChainElement::CallExpression(self.alloc(self.call_expression(
span,
Expand Down Expand Up @@ -12674,21 +12678,22 @@ impl<'a> AstBuilder<'a> {
/// - attributes: List of JSX attributes. In React-like applications, these become props.
/// - type_parameters: Type parameters for generic JSX elements.
#[inline]
pub fn jsx_opening_element<T1>(
pub fn jsx_opening_element<T1, T2>(
self,
span: Span,
self_closing: bool,
name: JSXElementName<'a>,
name: T1,
attributes: Vec<'a, JSXAttributeItem<'a>>,
type_parameters: T1,
type_parameters: T2,
) -> JSXOpeningElement<'a>
where
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
T1: IntoIn<'a, Box<'a, JSXElementName<'a>>>,
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
{
JSXOpeningElement {
span,
self_closing,
name,
name: name.into_in(self.allocator),
attributes,
type_parameters: type_parameters.into_in(self.allocator),
}
Expand All @@ -12705,16 +12710,17 @@ impl<'a> AstBuilder<'a> {
/// - attributes: List of JSX attributes. In React-like applications, these become props.
/// - type_parameters: Type parameters for generic JSX elements.
#[inline]
pub fn alloc_jsx_opening_element<T1>(
pub fn alloc_jsx_opening_element<T1, T2>(
self,
span: Span,
self_closing: bool,
name: JSXElementName<'a>,
name: T1,
attributes: Vec<'a, JSXAttributeItem<'a>>,
type_parameters: T1,
type_parameters: T2,
) -> Box<'a, JSXOpeningElement<'a>>
where
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
T1: IntoIn<'a, Box<'a, JSXElementName<'a>>>,
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
{
Box::new_in(
self.jsx_opening_element(span, self_closing, name, attributes, type_parameters),
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl<'a, 'b> IsConstant<'a, 'b> for Expression<'a> {

impl<'a, 'b> IsConstant<'a, 'b> for CallExpression<'a> {
fn is_constant(&self, _in_boolean_position: bool, ctx: &LintContext<'a>) -> bool {
if let Expression::Identifier(ident) = &self.callee {
if let Expression::Identifier(ident) = &*self.callee {
if ident.name == "Boolean"
&& self.arguments.iter().next().map_or(true, |first| first.is_constant(true, ctx))
{
Expand Down Expand Up @@ -408,7 +408,7 @@ pub fn is_global_require_call(call_expr: &CallExpression, ctx: &LintContext) ->
return false;
}

if let Expression::Identifier(id_ref) = &call_expr.callee {
if let Expression::Identifier(id_ref) = &*call_expr.callee {
id_ref.name == "require" && is_global_reference(id_ref, ctx)
} else {
false
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/func_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ impl Rule for FuncNames {
// check if the calling function is inside of its own body
// when yes remove it from invalid_funcs because recursion are always named
AstKind::CallExpression(expression) => {
if let Expression::Identifier(identifier) = &expression.callee {
if let Expression::Identifier(identifier) = &*expression.callee {
// check at first if the callee calls an invalid function
if !invalid_funcs
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl Rule for NoAlert {
};

let scope_id = node.scope_id();
let callee = &call_expr.callee;
let callee = &*call_expr.callee;

if let Expression::Identifier(ident) = callee {
let name = ident.name.as_str();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Rule for NoArrayConstructor {
let (span, callee, arguments, type_parameters, optional) = match node.kind() {
AstKind::CallExpression(call_expr) => (
call_expr.span,
&call_expr.callee,
&*call_expr.callee,
&call_expr.arguments,
&call_expr.type_parameters,
call_expr.optional,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl NoConstantBinaryExpression {
| Expression::UnaryExpression(_) => true,
expr if expr.is_literal() => true,
Expression::CallExpression(call_expr) => {
if let Expression::Identifier(ident) = &call_expr.callee {
if let Expression::Identifier(ident) = &*call_expr.callee {
return ["Boolean", "String", "Number"].contains(&ident.name.as_str())
&& ctx.semantic().is_reference_to_global_variable(ident);
}
Expand Down Expand Up @@ -290,7 +290,7 @@ impl NoConstantBinaryExpression {
_ => true,
},
Expression::CallExpression(call_expr) => {
if let Expression::Identifier(ident) = &call_expr.callee {
if let Expression::Identifier(ident) = &*call_expr.callee {
if ident.name == "String"
|| ident.name == "Number"
&& ctx.semantic().is_reference_to_global_variable(ident)
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_obj_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl Rule for NoObjCalls {
#[allow(clippy::needless_return)]
let (callee, span) = match node.kind() {
AstKind::NewExpression(expr) => (&expr.callee, expr.span),
AstKind::CallExpression(expr) => (&expr.callee, expr.span),
AstKind::CallExpression(expr) => (&*expr.callee, expr.span),
_ => return,
};

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/eslint/no_this_before_super.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ impl NoThisBeforeSuper {
match arg {
Argument::Super(_) | Argument::ThisExpression(_) => true,
Argument::CallExpression(call_expr) => {
matches!(&call_expr.callee, Expression::Super(_) | Expression::ThisExpression(_))
|| matches!(&call_expr.callee,
matches!(&*call_expr.callee, Expression::Super(_) | Expression::ThisExpression(_))
|| matches!(&*call_expr.callee,
Expression::StaticMemberExpression(static_member) if
matches!(static_member.object, Expression::Super(_) | Expression::ThisExpression(_)))
|| Self::contains_this_or_super_in_args(&call_expr.arguments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn is_single_super_call<'f, 'a: 'f>(body: &'f FunctionBody<'a>) -> Option<&'f Ca
let Statement::ExpressionStatement(expr) = &body.statements[0] else { return None };
let Expression::CallExpression(call) = &expr.expression else { return None };

matches!(call.callee, Expression::Super(_)).then(|| call.as_ref())
matches!(&*call.callee, Expression::Super(_)).then(|| call.as_ref())
}

/// Returns `false` if any parameter is an array/object unpacking binding or an
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/import/no_amd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Rule for NoAmd {
return;
}
if let AstKind::CallExpression(call_expr) = node.kind() {
if let Expression::Identifier(ref identifier) = &call_expr.callee {
if let Expression::Identifier(ref identifier) = &*call_expr.callee {
if identifier.name != "define" && identifier.name != "require" {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Rule for NoWebpackLoaderSyntax {

match node.kind() {
AstKind::CallExpression(call_expr) => {
if let Expression::Identifier(identifier) = &call_expr.callee {
if let Expression::Identifier(identifier) = &*call_expr.callee {
if identifier.name != "require" {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/max_expects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl MaxExpects {
let AstKind::CallExpression(call_expr) = node.kind() else {
return;
};
let Expression::Identifier(ident) = &call_expr.callee else {
let Expression::Identifier(ident) = &*call_expr.callee else {
return;
};

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/no_disabled_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>)
};
ctx.diagnostic(no_disabled_tests_diagnostic(error, help, call_expr.callee.span()));
}
} else if let Expression::Identifier(ident) = &call_expr.callee {
} else if let Expression::Identifier(ident) = &*call_expr.callee {
if ident.name.as_str() == "pending"
&& ctx.semantic().is_reference_to_global_variable(ident)
{
Expand Down
Loading
Loading