From 4f6dc22a3818e983c3bc5aa82cf73b383932d9c9 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Wed, 23 Oct 2024 13:26:23 +0000 Subject: [PATCH] refactor(transformer/react-refresh): avoid re-creating `Atom`s (#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. --- crates/oxc_transformer/src/react/refresh.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/crates/oxc_transformer/src/react/refresh.rs b/crates/oxc_transformer/src/react/refresh.rs index 8d37e51af9ba6..6119eb7f83ebb 100644 --- a/crates/oxc_transformer/src/react/refresh.rs +++ b/crates/oxc_transformer/src/react/refresh.rs @@ -102,7 +102,7 @@ pub struct ReactRefresh<'a, 'ctx> { emit_full_signatures: bool, ctx: &'ctx TransformCtx<'a>, // States - registrations: Vec<(SymbolId, Atom<'a>)>, + registrations: Vec<(BoundIdentifier<'a>, Atom<'a>)>, signature_declarator_items: Vec>>, /// Used to wrap call expression with signature. /// (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 new_statements = ctx.ast.vec_with_capacity(self.registrations.len() + 1); - for (symbol_id, persistent_id) in self.registrations.drain(..) { - let name = ctx.ast.atom(ctx.symbols().get_name(symbol_id)); - let binding = BoundIdentifier::new(name, symbol_id); + for (binding, persistent_id) in self.registrations.drain(..) { variable_declarator_items.push(ctx.ast.variable_declarator( SPAN, VariableDeclarationKind::Var, @@ -451,8 +449,9 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> { ctx: &mut TraverseCtx<'a>, ) -> AssignmentTarget<'a> { let binding = ctx.generate_uid_in_root_scope("c", SymbolFlags::FunctionScopedVariable); - self.registrations.push((binding.symbol_id, persistent_id)); - binding.create_target(reference_flags, ctx) + let target = binding.create_target(reference_flags, ctx); + self.registrations.push((binding, persistent_id)); + target } /// Similar to the `findInnerComponents` function in `react-refresh/babel`.