refactor(transformer/react-refresh): avoid re-creating Atoms (#6816)

Store `BoundIdentifier` in `registrations`. This allows removing the call to `ctx.ast.atom()` which re-allocates a new `Atom` into the arena - unnecessary as it's already been allocated when the UID was created.

It's unfortunate that `Semantic` stores symbol names as `CompactStr`s instead of `Atom`s, otherwise we could get the name as an `Atom` without re-allocating it. Hopefully we can make that change in future, and then reduce `BoundIdentifier` to just contain the `SymbolId`. But for now, we are kind of stuck with it.
This commit is contained in:
overlookmotel 2024-10-23 13:26:23 +00:00
parent 8316069e7d
commit 4f6dc22a38

View file

@ -102,7 +102,7 @@ pub struct ReactRefresh<'a, 'ctx> {
emit_full_signatures: bool, emit_full_signatures: bool,
ctx: &'ctx TransformCtx<'a>, ctx: &'ctx TransformCtx<'a>,
// States // States
registrations: Vec<(SymbolId, Atom<'a>)>, registrations: Vec<(BoundIdentifier<'a>, Atom<'a>)>,
signature_declarator_items: Vec<oxc_allocator::Vec<'a, VariableDeclarator<'a>>>, signature_declarator_items: Vec<oxc_allocator::Vec<'a, VariableDeclarator<'a>>>,
/// Used to wrap call expression with signature. /// Used to wrap call expression with signature.
/// (eg: hoc(() => {}) -> _s1(hoc(_s1(() => {})))) /// (eg: hoc(() => {}) -> _s1(hoc(_s1(() => {}))))
@ -154,9 +154,7 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> {
let mut variable_declarator_items = ctx.ast.vec_with_capacity(self.registrations.len()); let mut variable_declarator_items = ctx.ast.vec_with_capacity(self.registrations.len());
let mut new_statements = ctx.ast.vec_with_capacity(self.registrations.len() + 1); let mut new_statements = ctx.ast.vec_with_capacity(self.registrations.len() + 1);
for (symbol_id, persistent_id) in self.registrations.drain(..) { for (binding, persistent_id) in self.registrations.drain(..) {
let name = ctx.ast.atom(ctx.symbols().get_name(symbol_id));
let binding = BoundIdentifier::new(name, symbol_id);
variable_declarator_items.push(ctx.ast.variable_declarator( variable_declarator_items.push(ctx.ast.variable_declarator(
SPAN, SPAN,
VariableDeclarationKind::Var, VariableDeclarationKind::Var,
@ -451,8 +449,9 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> {
ctx: &mut TraverseCtx<'a>, ctx: &mut TraverseCtx<'a>,
) -> AssignmentTarget<'a> { ) -> AssignmentTarget<'a> {
let binding = ctx.generate_uid_in_root_scope("c", SymbolFlags::FunctionScopedVariable); let binding = ctx.generate_uid_in_root_scope("c", SymbolFlags::FunctionScopedVariable);
self.registrations.push((binding.symbol_id, persistent_id)); let target = binding.create_target(reference_flags, ctx);
binding.create_target(reference_flags, ctx) self.registrations.push((binding, persistent_id));
target
} }
/// Similar to the `findInnerComponents` function in `react-refresh/babel`. /// Similar to the `findInnerComponents` function in `react-refresh/babel`.