Skip to content
Merged
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
perf(transformer/using): avoid large types on stack (#9676)
Follow-on after #9310.

Holding large types on the stack is generally best to avoid where possible. Get the data into the arena as quickly as possible, and then only need to pass around `Box`es (which are only 8 bytes).

In the case of `Class`, previously we were `unbox`-ing a `Class` (pull it out of the arena, and onto the stack) and then allocating it back into the arena again. `Class` is a large type - 160 bytes - and this extra work doesn't add any value. We can just leave the `Class` where it is in the arena, and pass around a `Box<Class>`.

This is something of a micro-optimization, but they all add up...
  • Loading branch information
overlookmotel committed Mar 11, 2025
commit 15dd0d421847c0846e67582bb60f1aad79c37160
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use std::mem;

use rustc_hash::FxHashMap;

use oxc_allocator::{Address, GetAddress, Vec as ArenaVec};
use oxc_allocator::{Address, Box as ArenaBox, GetAddress, Vec as ArenaVec};
use oxc_ast::{NONE, ast::*};
use oxc_ecmascript::BoundNames;
use oxc_semantic::{ScopeFlags, ScopeId, SymbolFlags};
Expand Down Expand Up @@ -391,8 +391,7 @@ impl<'a> Traverse<'a> for ExplicitResourceManagement<'a, '_> {
let xx = BoundIdentifier::from_binding_ident(class_binding)
.create_read_reference(ctx);

inner_block
.push(Self::transform_class_decl(class_decl.unbox(), ctx));
inner_block.push(Self::transform_class_decl(class_decl, ctx));

let local = ModuleExportName::IdentifierReference(xx);
let exported = ctx
Expand Down Expand Up @@ -449,7 +448,7 @@ impl<'a> Traverse<'a> for ExplicitResourceManagement<'a, '_> {
));
}
Statement::ClassDeclaration(class_decl) => {
inner_block.push(Self::transform_class_decl(class_decl.unbox(), ctx));
inner_block.push(Self::transform_class_decl(class_decl, ctx));
}
Statement::VariableDeclaration(ref mut var_declaration) => {
if var_declaration.kind == VariableDeclarationKind::Using {
Expand Down Expand Up @@ -749,7 +748,7 @@ impl<'a> ExplicitResourceManagement<'a, '_> {
using_ctx: &BoundIdentifier<'a>,
parent_scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
) -> CatchClause<'a> {
) -> ArenaBox<'a, CatchClause<'a>> {
// catch (_) { _usingCtx.e = _; }
// ^ catch_parameter
// ^^^^^^^^^^^^^^^^^^^^^^^^ catch_scope_id
Expand Down Expand Up @@ -783,7 +782,7 @@ impl<'a> ExplicitResourceManagement<'a, '_> {
);

// `catch (_) { _usingCtx.e = _; }`
ctx.ast.catch_clause_with_scope_id(
ctx.ast.alloc_catch_clause_with_scope_id(
SPAN,
Some(catch_parameter),
ctx.ast.block_statement_with_scope_id(SPAN, ctx.ast.vec1(stmt), block_scope_id),
Expand All @@ -797,7 +796,7 @@ impl<'a> ExplicitResourceManagement<'a, '_> {
parent_scope_id: ScopeId,
needs_await: bool,
ctx: &mut TraverseCtx<'a>,
) -> BlockStatement<'a> {
) -> ArenaBox<'a, BlockStatement<'a>> {
let finally_scope_id = ctx.create_child_scope(parent_scope_id, ScopeFlags::empty());

// `_usingCtx.d()`
Expand All @@ -816,19 +815,22 @@ impl<'a> ExplicitResourceManagement<'a, '_> {

let stmt = if needs_await { ctx.ast.expression_await(SPAN, expr) } else { expr };

ctx.ast.block_statement_with_scope_id(
ctx.ast.alloc_block_statement_with_scope_id(
SPAN,
ctx.ast.vec1(ctx.ast.statement_expression(SPAN, stmt)),
finally_scope_id,
)
}

/// `class C {}` -> `var C = class {};`
fn transform_class_decl(mut class_decl: Class<'a>, ctx: &mut TraverseCtx<'a>) -> Statement<'a> {
fn transform_class_decl(
mut class_decl: ArenaBox<'a, Class<'a>>,
ctx: &mut TraverseCtx<'a>,
) -> Statement<'a> {
let id = class_decl.id.take().expect("ClassDeclaration should have an id");

class_decl.r#type = ClassType::ClassExpression;
let class_expr = Expression::ClassExpression(ctx.ast.alloc(class_decl));
let class_expr = Expression::ClassExpression(class_decl);

*ctx.scoping_mut().symbol_flags_mut(id.symbol_id()) = SymbolFlags::FunctionScopedVariable;

Expand Down
Loading