Skip to content

Commit 374050a

Browse files
committed
perf(transformer/jsx): pre-allocate enough memory for arguments of createElement/jsx function (#9915)
This is a hot path for JSX transform, allocating sufficient memory can avoid reserving in `push`, every bit of optimization that adds up can be huge.
1 parent fbb268a commit 374050a

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

crates/oxc_transformer/src/jsx/jsx_impl.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,9 @@ impl<'a> JsxImpl<'a, '_> {
559559
let is_development = self.options.development;
560560
let is_element = opening_element.is_some();
561561

562-
let mut arguments = ctx.ast.vec();
562+
// The maximum capacity length of arguments allowed.
563+
let capacity = if is_classic { 3 + children.len() } else { 6 };
564+
let mut arguments = ctx.ast.vec_with_capacity(capacity);
563565

564566
// The key prop in `<div key={true} />`
565567
let mut key_prop = None;
@@ -755,6 +757,7 @@ impl<'a> JsxImpl<'a, '_> {
755757
self.get_fragment(ctx)
756758
};
757759
arguments.insert(0, Argument::from(argument_expr));
760+
debug_assert!(arguments.len() <= capacity);
758761

759762
let callee = self.get_create_element(has_key_after_props_spread, need_jsxs, ctx);
760763
ctx.ast.expression_call_with_pure(span, callee, NONE, arguments, false, self.pure)

0 commit comments

Comments
 (0)