mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
perf(transformer): use AstBuilder::vec_from_array (#7333)
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.
This commit is contained in:
parent
39afb48025
commit
510b95d6c4
6 changed files with 46 additions and 60 deletions
|
|
@ -297,16 +297,14 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
|
||||||
|
|
||||||
{
|
{
|
||||||
// Modify the wrapper function to add new body, params, and scope_id.
|
// Modify the wrapper function to add new body, params, and scope_id.
|
||||||
let mut statements = ctx.ast.vec_with_capacity(3);
|
let async_to_gen_decl = self.create_async_to_generator_declaration(
|
||||||
let statement = self.create_async_to_generator_declaration(
|
|
||||||
&bound_ident,
|
&bound_ident,
|
||||||
params,
|
params,
|
||||||
body,
|
body,
|
||||||
generator_scope_id,
|
generator_scope_id,
|
||||||
ctx,
|
ctx,
|
||||||
);
|
);
|
||||||
statements.push(statement);
|
let statements = if has_function_id {
|
||||||
if has_function_id {
|
|
||||||
let id = caller_function.id.as_ref().unwrap();
|
let id = caller_function.id.as_ref().unwrap();
|
||||||
// If the function has an id, then we need to return the id.
|
// If the function has an id, then we need to return the id.
|
||||||
// `function foo() { ... }` -> `function foo() {} return foo;`
|
// `function foo() { ... }` -> `function foo() {} return foo;`
|
||||||
|
|
@ -316,17 +314,17 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
|
||||||
id.symbol_id(),
|
id.symbol_id(),
|
||||||
ReferenceFlags::Read,
|
ReferenceFlags::Read,
|
||||||
);
|
);
|
||||||
let statement = Statement::FunctionDeclaration(caller_function);
|
let func_decl = Statement::FunctionDeclaration(caller_function);
|
||||||
statements.push(statement);
|
let statement_return = ctx.ast.statement_return(SPAN, Some(reference));
|
||||||
statements.push(ctx.ast.statement_return(SPAN, Some(reference)));
|
ctx.ast.vec_from_array([async_to_gen_decl, func_decl, statement_return])
|
||||||
} else {
|
} else {
|
||||||
// If the function doesn't have an id, then we need to return the function itself.
|
// If the function doesn't have an id, then we need to return the function itself.
|
||||||
// `function() { ... }` -> `return function() { ... };`
|
// `function() { ... }` -> `return function() { ... };`
|
||||||
let statement_return = ctx
|
let statement_return = ctx
|
||||||
.ast
|
.ast
|
||||||
.statement_return(SPAN, Some(Expression::FunctionExpression(caller_function)));
|
.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());
|
debug_assert!(wrapper_function.body.is_none());
|
||||||
wrapper_function.r#async = false;
|
wrapper_function.r#async = false;
|
||||||
wrapper_function.generator = false;
|
wrapper_function.generator = false;
|
||||||
|
|
@ -382,15 +380,16 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
|
||||||
|
|
||||||
// function _name() { _ref.apply(this, arguments); }
|
// function _name() { _ref.apply(this, arguments); }
|
||||||
{
|
{
|
||||||
let mut statements = ctx.ast.vec_with_capacity(2);
|
let statements = ctx.ast.vec_from_array([
|
||||||
statements.push(self.create_async_to_generator_assignment(
|
self.create_async_to_generator_assignment(
|
||||||
&bound_ident,
|
&bound_ident,
|
||||||
params,
|
params,
|
||||||
body,
|
body,
|
||||||
generator_scope_id,
|
generator_scope_id,
|
||||||
ctx,
|
ctx,
|
||||||
));
|
),
|
||||||
statements.push(Self::create_apply_call_statement(&bound_ident, ctx));
|
Self::create_apply_call_statement(&bound_ident, ctx),
|
||||||
|
]);
|
||||||
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), statements);
|
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);
|
let scope_id = ctx.create_child_scope(ctx.current_scope_id(), ScopeFlags::Function);
|
||||||
|
|
@ -468,9 +467,7 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
|
||||||
generator_function_id,
|
generator_function_id,
|
||||||
ctx,
|
ctx,
|
||||||
);
|
);
|
||||||
let mut statements = ctx.ast.vec_with_capacity(2);
|
let statements = ctx.ast.vec_from_array([statement, caller_function]);
|
||||||
statements.push(statement);
|
|
||||||
statements.push(caller_function);
|
|
||||||
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), statements);
|
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), statements);
|
||||||
let params = Self::create_empty_params(ctx);
|
let params = Self::create_empty_params(ctx);
|
||||||
let wrapper_function = Self::create_function(None, params, body, wrapper_scope_id, ctx);
|
let wrapper_function = Self::create_function(None, params, body, wrapper_scope_id, ctx);
|
||||||
|
|
@ -604,9 +601,8 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
|
||||||
));
|
));
|
||||||
|
|
||||||
// (this, arguments)
|
// (this, arguments)
|
||||||
let mut arguments = ctx.ast.vec_with_capacity(2);
|
let this = Argument::from(ctx.ast.expression_this(SPAN));
|
||||||
arguments.push(Argument::from(ctx.ast.expression_this(SPAN)));
|
let arguments = ctx.ast.vec_from_array([this, arguments_ident]);
|
||||||
arguments.push(arguments_ident);
|
|
||||||
// _ref.apply
|
// _ref.apply
|
||||||
let callee = Expression::from(ctx.ast.member_expression_static(
|
let callee = Expression::from(ctx.ast.member_expression_static(
|
||||||
SPAN,
|
SPAN,
|
||||||
|
|
|
||||||
|
|
@ -261,24 +261,22 @@ impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
|
||||||
Some(ctx.ast.for_statement_init_variable_declaration(
|
Some(ctx.ast.for_statement_init_variable_declaration(
|
||||||
SPAN,
|
SPAN,
|
||||||
VariableDeclarationKind::Var,
|
VariableDeclarationKind::Var,
|
||||||
{
|
ctx.ast.vec_from_array([
|
||||||
let mut items = ctx.ast.vec_with_capacity(2);
|
ctx.ast.variable_declarator(
|
||||||
items.push(ctx.ast.variable_declarator(
|
|
||||||
SPAN,
|
SPAN,
|
||||||
VariableDeclarationKind::Var,
|
VariableDeclarationKind::Var,
|
||||||
iterator_key.create_binding_pattern(ctx),
|
iterator_key.create_binding_pattern(ctx),
|
||||||
Some(iterator),
|
Some(iterator),
|
||||||
false,
|
false,
|
||||||
));
|
),
|
||||||
items.push(ctx.ast.variable_declarator(
|
ctx.ast.variable_declarator(
|
||||||
SPAN,
|
SPAN,
|
||||||
VariableDeclarationKind::Var,
|
VariableDeclarationKind::Var,
|
||||||
step_key.create_binding_pattern(ctx),
|
step_key.create_binding_pattern(ctx),
|
||||||
None,
|
None,
|
||||||
false,
|
false,
|
||||||
));
|
),
|
||||||
items
|
]),
|
||||||
},
|
|
||||||
false,
|
false,
|
||||||
)),
|
)),
|
||||||
Some(ctx.ast.expression_assignment(
|
Some(ctx.ast.expression_assignment(
|
||||||
|
|
@ -371,9 +369,8 @@ impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
|
||||||
{
|
{
|
||||||
ctx.ast.block_statement_with_scope_id(
|
ctx.ast.block_statement_with_scope_id(
|
||||||
SPAN,
|
SPAN,
|
||||||
{
|
ctx.ast.vec_from_array([
|
||||||
let mut items = ctx.ast.vec_with_capacity(2);
|
ctx.ast.statement_expression(
|
||||||
items.push(ctx.ast.statement_expression(
|
|
||||||
SPAN,
|
SPAN,
|
||||||
ctx.ast.expression_assignment(
|
ctx.ast.expression_assignment(
|
||||||
SPAN,
|
SPAN,
|
||||||
|
|
@ -381,8 +378,8 @@ impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
|
||||||
iterator_had_error_key.create_write_target(ctx),
|
iterator_had_error_key.create_write_target(ctx),
|
||||||
ctx.ast.expression_boolean_literal(SPAN, true),
|
ctx.ast.expression_boolean_literal(SPAN, true),
|
||||||
),
|
),
|
||||||
));
|
),
|
||||||
items.push(ctx.ast.statement_expression(
|
ctx.ast.statement_expression(
|
||||||
SPAN,
|
SPAN,
|
||||||
ctx.ast.expression_assignment(
|
ctx.ast.expression_assignment(
|
||||||
SPAN,
|
SPAN,
|
||||||
|
|
@ -390,9 +387,8 @@ impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
|
||||||
iterator_error_key.create_write_target(ctx),
|
iterator_error_key.create_write_target(ctx),
|
||||||
err_ident.create_read_expression(ctx),
|
err_ident.create_read_expression(ctx),
|
||||||
),
|
),
|
||||||
));
|
),
|
||||||
items
|
]),
|
||||||
},
|
|
||||||
block_scope_id,
|
block_scope_id,
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -176,10 +176,7 @@ impl<'a, 'ctx> JsxSource<'a, 'ctx> {
|
||||||
.object_property_kind_object_property(SPAN, kind, key, value, false, false, false)
|
.object_property_kind_object_property(SPAN, kind, key, value, false, false, false)
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut properties = ctx.ast.vec_with_capacity(3);
|
let properties = ctx.ast.vec_from_array([filename, line_number, column_number]);
|
||||||
properties.push(filename);
|
|
||||||
properties.push(line_number);
|
|
||||||
properties.push(column_number);
|
|
||||||
ctx.ast.expression_object(SPAN, properties, None)
|
ctx.ast.expression_object(SPAN, properties, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -164,11 +164,12 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> {
|
||||||
));
|
));
|
||||||
|
|
||||||
let callee = self.refresh_reg.to_expression(ctx);
|
let callee = self.refresh_reg.to_expression(ctx);
|
||||||
let mut arguments = ctx.ast.vec_with_capacity(2);
|
let arguments = ctx.ast.vec_from_array([
|
||||||
arguments.push(Argument::from(binding.create_read_expression(ctx)));
|
Argument::from(binding.create_read_expression(ctx)),
|
||||||
arguments.push(Argument::from(
|
Argument::from(
|
||||||
ctx.ast.expression_string_literal(SPAN, ctx.ast.atom(&persistent_id)),
|
ctx.ast.expression_string_literal(SPAN, ctx.ast.atom(&persistent_id)),
|
||||||
));
|
),
|
||||||
|
]);
|
||||||
new_statements.push(ctx.ast.statement_expression(
|
new_statements.push(ctx.ast.statement_expression(
|
||||||
SPAN,
|
SPAN,
|
||||||
ctx.ast.expression_call(SPAN, callee, NONE, arguments, false),
|
ctx.ast.expression_call(SPAN, callee, NONE, arguments, false),
|
||||||
|
|
|
||||||
|
|
@ -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())
|
ctx.create_ident_expr(SPAN, Atom::from("RegExp"), symbol_id, ReferenceFlags::read())
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut arguments = ctx.ast.vec_with_capacity(2);
|
let arguments = ctx.ast.vec_from_array([
|
||||||
arguments.push(Argument::from(ctx.ast.expression_string_literal(SPAN, pattern_source)));
|
Argument::from(ctx.ast.expression_string_literal(SPAN, pattern_source)),
|
||||||
|
Argument::from(ctx.ast.expression_string_literal(SPAN, flags.to_string())),
|
||||||
let flags_str = flags.to_string();
|
]);
|
||||||
let flags_str = Argument::from(ctx.ast.expression_string_literal(SPAN, flags_str));
|
|
||||||
arguments.push(flags_str);
|
|
||||||
|
|
||||||
*expr = ctx.ast.expression_new(regexp.span, callee, arguments, NONE);
|
*expr = ctx.ast.expression_new(regexp.span, callee, arguments, NONE);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -491,12 +491,10 @@ impl<'a, 'ctx> TypeScriptNamespace<'a, 'ctx> {
|
||||||
assignments.push(Self::create_assignment_statement(name.clone(), id.name.clone(), ctx));
|
assignments.push(Self::create_assignment_statement(name.clone(), id.name.clone(), ctx));
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut stmts = ctx.ast.vec_with_capacity(2);
|
ctx.ast.vec_from_array([
|
||||||
stmts.push(Statement::VariableDeclaration(var_decl));
|
Statement::VariableDeclaration(var_decl),
|
||||||
stmts.push(
|
|
||||||
ctx.ast.statement_expression(SPAN, ctx.ast.expression_sequence(SPAN, assignments)),
|
ctx.ast.statement_expression(SPAN, ctx.ast.expression_sequence(SPAN, assignments)),
|
||||||
);
|
])
|
||||||
stmts
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue