Skip to content

Commit

Permalink
perf(transformer): use AstBuilder::vec_from_array (#7333)
Browse files Browse the repository at this point in the history
Use `AstBuilder::vec_from_array` introduced in #7331 in the transformer, in place of creating a `Vec` with `Vec::with_capacity` and then pushing values to it.
  • Loading branch information
overlookmotel committed Nov 18, 2024
1 parent 39afb48 commit 510b95d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 60 deletions.
44 changes: 20 additions & 24 deletions crates/oxc_transformer/src/es2017/async_to_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,14 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {

{
// Modify the wrapper function to add new body, params, and scope_id.
let mut statements = ctx.ast.vec_with_capacity(3);
let statement = self.create_async_to_generator_declaration(
let async_to_gen_decl = self.create_async_to_generator_declaration(
&bound_ident,
params,
body,
generator_scope_id,
ctx,
);
statements.push(statement);
if has_function_id {
let statements = if has_function_id {
let id = caller_function.id.as_ref().unwrap();
// If the function has an id, then we need to return the id.
// `function foo() { ... }` -> `function foo() {} return foo;`
Expand All @@ -316,17 +314,17 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
id.symbol_id(),
ReferenceFlags::Read,
);
let statement = Statement::FunctionDeclaration(caller_function);
statements.push(statement);
statements.push(ctx.ast.statement_return(SPAN, Some(reference)));
let func_decl = Statement::FunctionDeclaration(caller_function);
let statement_return = ctx.ast.statement_return(SPAN, Some(reference));
ctx.ast.vec_from_array([async_to_gen_decl, func_decl, statement_return])
} else {
// If the function doesn't have an id, then we need to return the function itself.
// `function() { ... }` -> `return function() { ... };`
let statement_return = ctx
.ast
.statement_return(SPAN, Some(Expression::FunctionExpression(caller_function)));
statements.push(statement_return);
}
ctx.ast.vec_from_array([async_to_gen_decl, statement_return])
};
debug_assert!(wrapper_function.body.is_none());
wrapper_function.r#async = false;
wrapper_function.generator = false;
Expand Down Expand Up @@ -382,15 +380,16 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {

// function _name() { _ref.apply(this, arguments); }
{
let mut statements = ctx.ast.vec_with_capacity(2);
statements.push(self.create_async_to_generator_assignment(
&bound_ident,
params,
body,
generator_scope_id,
ctx,
));
statements.push(Self::create_apply_call_statement(&bound_ident, ctx));
let statements = ctx.ast.vec_from_array([
self.create_async_to_generator_assignment(
&bound_ident,
params,
body,
generator_scope_id,
ctx,
),
Self::create_apply_call_statement(&bound_ident, ctx),
]);
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), statements);

let scope_id = ctx.create_child_scope(ctx.current_scope_id(), ScopeFlags::Function);
Expand Down Expand Up @@ -468,9 +467,7 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
generator_function_id,
ctx,
);
let mut statements = ctx.ast.vec_with_capacity(2);
statements.push(statement);
statements.push(caller_function);
let statements = ctx.ast.vec_from_array([statement, caller_function]);
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), statements);
let params = Self::create_empty_params(ctx);
let wrapper_function = Self::create_function(None, params, body, wrapper_scope_id, ctx);
Expand Down Expand Up @@ -604,9 +601,8 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
));

// (this, arguments)
let mut arguments = ctx.ast.vec_with_capacity(2);
arguments.push(Argument::from(ctx.ast.expression_this(SPAN)));
arguments.push(arguments_ident);
let this = Argument::from(ctx.ast.expression_this(SPAN));
let arguments = ctx.ast.vec_from_array([this, arguments_ident]);
// _ref.apply
let callee = Expression::from(ctx.ast.member_expression_static(
SPAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,22 @@ impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
Some(ctx.ast.for_statement_init_variable_declaration(
SPAN,
VariableDeclarationKind::Var,
{
let mut items = ctx.ast.vec_with_capacity(2);
items.push(ctx.ast.variable_declarator(
ctx.ast.vec_from_array([
ctx.ast.variable_declarator(
SPAN,
VariableDeclarationKind::Var,
iterator_key.create_binding_pattern(ctx),
Some(iterator),
false,
));
items.push(ctx.ast.variable_declarator(
),
ctx.ast.variable_declarator(
SPAN,
VariableDeclarationKind::Var,
step_key.create_binding_pattern(ctx),
None,
false,
));
items
},
),
]),
false,
)),
Some(ctx.ast.expression_assignment(
Expand Down Expand Up @@ -371,28 +369,26 @@ impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
{
ctx.ast.block_statement_with_scope_id(
SPAN,
{
let mut items = ctx.ast.vec_with_capacity(2);
items.push(ctx.ast.statement_expression(
ctx.ast.vec_from_array([
ctx.ast.statement_expression(
SPAN,
ctx.ast.expression_assignment(
SPAN,
AssignmentOperator::Assign,
iterator_had_error_key.create_write_target(ctx),
ctx.ast.expression_boolean_literal(SPAN, true),
),
));
items.push(ctx.ast.statement_expression(
),
ctx.ast.statement_expression(
SPAN,
ctx.ast.expression_assignment(
SPAN,
AssignmentOperator::Assign,
iterator_error_key.create_write_target(ctx),
err_ident.create_read_expression(ctx),
),
));
items
},
),
]),
block_scope_id,
)
},
Expand Down
5 changes: 1 addition & 4 deletions crates/oxc_transformer/src/jsx/jsx_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,7 @@ impl<'a, 'ctx> JsxSource<'a, 'ctx> {
.object_property_kind_object_property(SPAN, kind, key, value, false, false, false)
};

let mut properties = ctx.ast.vec_with_capacity(3);
properties.push(filename);
properties.push(line_number);
properties.push(column_number);
let properties = ctx.ast.vec_from_array([filename, line_number, column_number]);
ctx.ast.expression_object(SPAN, properties, None)
}

Expand Down
11 changes: 6 additions & 5 deletions crates/oxc_transformer/src/jsx/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,12 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> {
));

let callee = self.refresh_reg.to_expression(ctx);
let mut arguments = ctx.ast.vec_with_capacity(2);
arguments.push(Argument::from(binding.create_read_expression(ctx)));
arguments.push(Argument::from(
ctx.ast.expression_string_literal(SPAN, ctx.ast.atom(&persistent_id)),
));
let arguments = ctx.ast.vec_from_array([
Argument::from(binding.create_read_expression(ctx)),
Argument::from(
ctx.ast.expression_string_literal(SPAN, ctx.ast.atom(&persistent_id)),
),
]);
new_statements.push(ctx.ast.statement_expression(
SPAN,
ctx.ast.expression_call(SPAN, callee, NONE, arguments, false),
Expand Down
10 changes: 4 additions & 6 deletions crates/oxc_transformer/src/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,10 @@ impl<'a, 'ctx> Traverse<'a> for RegExp<'a, 'ctx> {
ctx.create_ident_expr(SPAN, Atom::from("RegExp"), symbol_id, ReferenceFlags::read())
};

let mut arguments = ctx.ast.vec_with_capacity(2);
arguments.push(Argument::from(ctx.ast.expression_string_literal(SPAN, pattern_source)));

let flags_str = flags.to_string();
let flags_str = Argument::from(ctx.ast.expression_string_literal(SPAN, flags_str));
arguments.push(flags_str);
let arguments = ctx.ast.vec_from_array([
Argument::from(ctx.ast.expression_string_literal(SPAN, pattern_source)),
Argument::from(ctx.ast.expression_string_literal(SPAN, flags.to_string())),
]);

*expr = ctx.ast.expression_new(regexp.span, callee, arguments, NONE);
}
Expand Down
8 changes: 3 additions & 5 deletions crates/oxc_transformer/src/typescript/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,10 @@ impl<'a, 'ctx> TypeScriptNamespace<'a, 'ctx> {
assignments.push(Self::create_assignment_statement(name.clone(), id.name.clone(), ctx));
});

let mut stmts = ctx.ast.vec_with_capacity(2);
stmts.push(Statement::VariableDeclaration(var_decl));
stmts.push(
ctx.ast.vec_from_array([
Statement::VariableDeclaration(var_decl),
ctx.ast.statement_expression(SPAN, ctx.ast.expression_sequence(SPAN, assignments)),
);
stmts
])
}
}

Expand Down

0 comments on commit 510b95d

Please sign in to comment.