Unverified Commit 9d2d3937 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

perf: Make `cjs_optimizer` use `Atom` instead of `String` (#75588)

### What?

### Why?

`swc_atoms::Atom` is better for repeatedly used strings.
parent aa9ded6a
...@@ -3,6 +3,7 @@ use async_trait::async_trait; ...@@ -3,6 +3,7 @@ use async_trait::async_trait;
use next_custom_transforms::transforms::cjs_optimizer::{cjs_optimizer, Config, PackageConfig}; use next_custom_transforms::transforms::cjs_optimizer::{cjs_optimizer, Config, PackageConfig};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use swc_core::{ use swc_core::{
atoms::atom,
common::SyntaxContext, common::SyntaxContext,
ecma::{ast::*, visit::VisitMutWith}, ecma::{ast::*, visit::VisitMutWith},
}; };
...@@ -18,30 +19,30 @@ pub fn get_next_cjs_optimizer_rule(enable_mdx_rs: bool) -> ModuleRule { ...@@ -18,30 +19,30 @@ pub fn get_next_cjs_optimizer_rule(enable_mdx_rs: bool) -> ModuleRule {
// build it internally without accepting customization. // build it internally without accepting customization.
let config = Config { let config = Config {
packages: FxHashMap::from_iter([( packages: FxHashMap::from_iter([(
"next/server".to_string(), atom!("next/server"),
PackageConfig { PackageConfig {
transforms: FxHashMap::from_iter([ transforms: FxHashMap::from_iter([
( (
"NextRequest".into(), atom!("NextRequest"),
"next/dist/server/web/spec-extension/request".into(), atom!("next/dist/server/web/spec-extension/request"),
), ),
( (
"NextResponse".into(), atom!("NextResponse"),
"next/dist/server/web/spec-extension/response".into(), atom!("next/dist/server/web/spec-extension/response"),
), ),
( (
"ImageResponse".into(), atom!("ImageResponse"),
"next/dist/server/web/spec-extension/image-response".into(), atom!("next/dist/server/web/spec-extension/image-response"),
), ),
( (
"userAgentFromString".into(), atom!("userAgentFromString"),
"next/dist/server/web/spec-extension/user-agent".into(), atom!("next/dist/server/web/spec-extension/user-agent"),
), ),
( (
"userAgent".into(), atom!("userAgent"),
"next/dist/server/web/spec-extension/user-agent".into(), atom!("next/dist/server/web/spec-extension/user-agent"),
), ),
("after".into(), "next/dist/server/after".into()), (atom!("after"), atom!("next/dist/server/after")),
]), ]),
}, },
)]), )]),
......
...@@ -7,7 +7,7 @@ use swc_core::{ ...@@ -7,7 +7,7 @@ use swc_core::{
CallExpr, Callee, Decl, Expr, Id, Ident, IdentName, Lit, MemberExpr, MemberProp, CallExpr, Callee, Decl, Expr, Id, Ident, IdentName, Lit, MemberExpr, MemberProp,
Module, ModuleItem, Pat, Script, Stmt, VarDecl, VarDeclKind, VarDeclarator, Module, ModuleItem, Pat, Script, Stmt, VarDecl, VarDeclKind, VarDeclarator,
}, },
atoms::{Atom, JsWord}, atoms::Atom,
utils::{prepend_stmts, private_ident, ExprFactory, IdentRenamer}, utils::{prepend_stmts, private_ident, ExprFactory, IdentRenamer},
visit::{noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith}, visit::{noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith},
}, },
...@@ -23,18 +23,18 @@ pub fn cjs_optimizer(config: Config, unresolved_ctxt: SyntaxContext) -> CjsOptim ...@@ -23,18 +23,18 @@ pub fn cjs_optimizer(config: Config, unresolved_ctxt: SyntaxContext) -> CjsOptim
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
pub struct Config { pub struct Config {
pub packages: FxHashMap<String, PackageConfig>, pub packages: FxHashMap<Atom, PackageConfig>,
} }
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct PackageConfig { pub struct PackageConfig {
pub transforms: FxHashMap<JsWord, JsWord>, pub transforms: FxHashMap<Atom, Atom>,
} }
pub struct CjsOptimizer { pub struct CjsOptimizer {
data: State, data: State,
packages: FxHashMap<String, PackageConfig>, packages: FxHashMap<Atom, PackageConfig>,
unresolved_ctxt: SyntaxContext, unresolved_ctxt: SyntaxContext,
} }
...@@ -46,7 +46,7 @@ struct State { ...@@ -46,7 +46,7 @@ struct State {
imports: FxHashMap<Id, ImportRecord>, imports: FxHashMap<Id, ImportRecord>,
/// `(module_specifier, property): (identifier)` /// `(module_specifier, property): (identifier)`
replaced: FxHashMap<(Atom, JsWord), Id>, replaced: FxHashMap<(Atom, Atom), Id>,
extra_stmts: Vec<Stmt>, extra_stmts: Vec<Stmt>,
...@@ -64,7 +64,7 @@ struct ImportRecord { ...@@ -64,7 +64,7 @@ struct ImportRecord {
} }
impl CjsOptimizer { impl CjsOptimizer {
fn should_rewrite(&self, module_specifier: &str) -> Option<&FxHashMap<JsWord, JsWord>> { fn should_rewrite(&self, module_specifier: &Atom) -> Option<&FxHashMap<Atom, Atom>> {
self.packages.get(module_specifier).map(|v| &v.transforms) self.packages.get(module_specifier).map(|v| &v.transforms)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment