Skip to content

Commit

Permalink
perf: Make server action pass use Atom instead of String (#75594)
Browse files Browse the repository at this point in the history
### What?

Use `swc_atoms::Atom` instead of `String` in `next-swc`. 

### Why?

`Atom` does not allocate if another instance has the same value, meaning that `clone()` is a single atomic operation. But `String` allocates regardless of any other occurrence.
  • Loading branch information
kdy1 authored Feb 4, 2025
1 parent 361d4cf commit 49b93c6
Showing 1 changed file with 33 additions and 34 deletions.
67 changes: 33 additions & 34 deletions crates/next-custom-transforms/src/transforms/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
use serde::Deserialize;
use sha1::{Digest, Sha1};
use swc_core::{
atoms::Atom,
common::{
comments::{Comment, CommentKind, Comments},
errors::HANDLER,
Expand All @@ -20,7 +21,6 @@ use swc_core::{
},
ecma::{
ast::*,
atoms::JsWord,
utils::{private_ident, quote_ident, ExprFactory},
visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith},
},
Expand Down Expand Up @@ -116,7 +116,7 @@ enum ServerActionsErrorKind {

/// A mapping of hashed action id to the action's exported function name.
// Using BTreeMap to ensure the order of the actions is deterministic.
pub type ActionsMap = BTreeMap<String, String>;
pub type ActionsMap = BTreeMap<Atom, Atom>;

#[tracing::instrument(level = tracing::Level::TRACE, skip_all)]
pub fn server_actions<C: Comments>(
Expand Down Expand Up @@ -206,14 +206,14 @@ struct ServerActions<C: Comments> {

exported_idents: Vec<(
/* ident */ Ident,
/* name */ String,
/* id */ String,
/* name */ Atom,
/* id */ Atom,
)>,

annotations: Vec<Stmt>,
extra_items: Vec<ModuleItem>,
hoisted_extra_items: Vec<ModuleItem>,
export_actions: Vec<(/* name */ String, /* id */ String)>,
export_actions: Vec<(/* name */ Atom, /* id */ Atom)>,

private_ctxt: SyntaxContext,

Expand All @@ -229,7 +229,7 @@ impl<C: Comments> ServerActions<C> {
export_name: &str,
is_cache: bool,
params: Option<&Vec<Param>>,
) -> String {
) -> Atom {
// Attach a checksum to the action using sha1:
// $$id = special_byte + sha1('hash_salt' + 'file_name' + ':' + 'export_name');
// Currently encoded as hex.
Expand Down Expand Up @@ -304,23 +304,23 @@ impl<C: Comments> ServerActions<C> {
result.push((type_bit << 7) | (arg_mask << 1) | rest_args);
result.rotate_right(1);

hex_encode(result)
Atom::from(hex_encode(result))
}

fn gen_action_ident(&mut self) -> JsWord {
let id: JsWord = format!("$$RSC_SERVER_ACTION_{0}", self.reference_index).into();
fn gen_action_ident(&mut self) -> Atom {
let id: Atom = format!("$$RSC_SERVER_ACTION_{0}", self.reference_index).into();
self.reference_index += 1;
id
}

fn gen_cache_ident(&mut self) -> JsWord {
let id: JsWord = format!("$$RSC_SERVER_CACHE_{0}", self.reference_index).into();
fn gen_cache_ident(&mut self) -> Atom {
let id: Atom = format!("$$RSC_SERVER_CACHE_{0}", self.reference_index).into();
self.reference_index += 1;
id
}

fn gen_ref_ident(&mut self) -> JsWord {
let id: JsWord = format!("$$RSC_SERVER_REF_{0}", self.reference_index).into();
fn gen_ref_ident(&mut self) -> Atom {
let id: Atom = format!("$$RSC_SERVER_REF_{0}", self.reference_index).into();
self.reference_index += 1;
id
}
Expand Down Expand Up @@ -426,13 +426,13 @@ impl<C: Comments> ServerActions<C> {
new_params.push(Param::from(p.clone()));
}

let action_name = self.gen_action_ident().to_string();
let action_ident = Ident::new(action_name.clone().into(), arrow.span, self.private_ctxt);
let action_name = self.gen_action_ident();
let action_ident = Ident::new(action_name.clone(), arrow.span, self.private_ctxt);
let action_id = self.generate_server_reference_id(&action_name, false, Some(&new_params));

self.has_action = true;
self.export_actions
.push((action_name.to_string(), action_id.clone()));
.push((action_name.clone(), action_id.clone()));

let register_action_expr = bind_args_to_ref_expr(
annotate_ident_as_server_reference(
Expand Down Expand Up @@ -569,13 +569,13 @@ impl<C: Comments> ServerActions<C> {

new_params.append(&mut function.params);

let action_name: JsWord = self.gen_action_ident();
let action_name: Atom = self.gen_action_ident();
let action_ident = Ident::new(action_name.clone(), function.span, self.private_ctxt);
let action_id = self.generate_server_reference_id(&action_name, false, Some(&new_params));

self.has_action = true;
self.export_actions
.push((action_name.to_string(), action_id.clone()));
.push((action_name.clone(), action_id.clone()));

let register_action_expr = bind_args_to_ref_expr(
annotate_ident_as_server_reference(
Expand Down Expand Up @@ -690,15 +690,15 @@ impl<C: Comments> ServerActions<C> {
new_params.push(Param::from(p.clone()));
}

let cache_name: JsWord = self.gen_cache_ident();
let cache_name: Atom = self.gen_cache_ident();
let cache_ident = private_ident!(cache_name.clone());
let export_name: JsWord = cache_name;
let export_name: Atom = cache_name;

let reference_id = self.generate_server_reference_id(&export_name, true, Some(&new_params));

self.has_cache = true;
self.export_actions
.push((export_name.to_string(), reference_id.clone()));
.push((export_name.clone(), reference_id.clone()));

if let BlockStmtOrExpr::BlockStmt(block) = &mut *arrow.body {
block.visit_mut_with(&mut ClosureReplacer {
Expand Down Expand Up @@ -825,14 +825,14 @@ impl<C: Comments> ServerActions<C> {
new_params.push(p.clone());
}

let cache_name: JsWord = self.gen_cache_ident();
let cache_name: Atom = self.gen_cache_ident();
let cache_ident = private_ident!(cache_name.clone());

let reference_id = self.generate_server_reference_id(&cache_name, true, Some(&new_params));

self.has_cache = true;
self.export_actions
.push((cache_name.to_string(), reference_id.clone()));
.push((cache_name.clone(), reference_id.clone()));

let register_action_expr = annotate_ident_as_server_reference(
cache_ident.clone(),
Expand Down Expand Up @@ -1475,7 +1475,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
if !(is_cache_fn && self.config.is_react_server_layer) {
self.exported_idents.push((
f.ident.clone(),
f.ident.sym.to_string(),
f.ident.sym.clone(),
self.generate_server_reference_id(
f.ident.sym.as_ref(),
ref_id,
Expand All @@ -1492,7 +1492,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
for ident in &idents {
self.exported_idents.push((
ident.clone(),
ident.sym.to_string(),
ident.sym.clone(),
self.generate_server_reference_id(
ident.sym.as_ref(),
in_cache_file,
Expand Down Expand Up @@ -1533,7 +1533,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
// export { foo as bar }
self.exported_idents.push((
ident.clone(),
sym.to_string(),
sym.clone(),
self.generate_server_reference_id(
sym.as_ref(),
in_cache_file,
Expand All @@ -1544,7 +1544,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
// export { foo as "bar" }
self.exported_idents.push((
ident.clone(),
str.value.to_string(),
str.value.clone(),
self.generate_server_reference_id(
str.value.as_ref(),
in_cache_file,
Expand All @@ -1556,7 +1556,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
// export { foo }
self.exported_idents.push((
ident.clone(),
ident.sym.to_string(),
ident.sym.clone(),
self.generate_server_reference_id(
ident.sym.as_ref(),
in_cache_file,
Expand Down Expand Up @@ -1887,8 +1887,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(
IdentName::new(export_name.clone().into(), ident.span)
.into(),
IdentName::new(export_name.clone(), ident.span).into(),
),
init: Some(Box::new(Expr::Call(CallExpr {
span: call_expr_span,
Expand Down Expand Up @@ -1916,7 +1915,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
span: DUMMY_SP,
expr: Box::new(annotate_ident_as_server_reference(
ident.clone(),
ref_id.to_string(),
ref_id.clone(),
ident.span,
&self.comments,
)),
Expand Down Expand Up @@ -2336,7 +2335,7 @@ fn assign_arrow_expr(ident: &Ident, expr: Expr) -> Expr {

fn annotate_ident_as_server_reference(
ident: Ident,
action_id: String,
action_id: Atom,
original_span: Span,
comments: &dyn Comments,
) -> Expr {
Expand Down Expand Up @@ -2373,7 +2372,7 @@ fn annotate_ident_as_server_reference(
})
}

fn bind_args_to_ref_expr(expr: Expr, bound: Vec<Option<ExprOrSpread>>, action_id: String) -> Expr {
fn bind_args_to_ref_expr(expr: Expr, bound: Vec<Option<ExprOrSpread>>, action_id: Atom) -> Expr {
if bound.is_empty() {
expr
} else {
Expand Down Expand Up @@ -2842,7 +2841,7 @@ impl VisitMut for ClosureReplacer<'_> {

#[derive(Debug, Clone, PartialEq, Eq)]
struct NamePart {
prop: JsWord,
prop: Atom,
is_member: bool,
optional: bool,
}
Expand Down

0 comments on commit 49b93c6

Please sign in to comment.