From 0ef1d59c0b77b6769c3b625f291c184598415538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 23 Jul 2024 14:08:31 +0900 Subject: [PATCH 1/7] Drop atomicbool --- crates/swc_ecma_transforms_base/src/helpers/mod.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/swc_ecma_transforms_base/src/helpers/mod.rs b/crates/swc_ecma_transforms_base/src/helpers/mod.rs index 412b6a8fe23b..04bc003fde6d 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/mod.rs +++ b/crates/swc_ecma_transforms_base/src/helpers/mod.rs @@ -1,7 +1,4 @@ -use std::{ - mem::replace, - sync::atomic::{AtomicBool, Ordering}, -}; +use std::mem::replace; use once_cell::sync::Lazy; use rustc_hash::FxHashMap; @@ -142,7 +139,7 @@ macro_rules! define_helpers { ) => { #[derive(Debug,Default)] struct Inner { - $( $name: AtomicBool, )* + $( $name: bool, )* } impl Helpers { From 01f19c4e403e3ff4078474fb202ab5f751764a2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 23 Jul 2024 14:09:43 +0900 Subject: [PATCH 2/7] fix --- crates/swc_ecma_transforms_base/src/helpers/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/swc_ecma_transforms_base/src/helpers/mod.rs b/crates/swc_ecma_transforms_base/src/helpers/mod.rs index 04bc003fde6d..af78c0f4fa84 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/mod.rs +++ b/crates/swc_ecma_transforms_base/src/helpers/mod.rs @@ -144,8 +144,8 @@ macro_rules! define_helpers { impl Helpers { $( - pub fn $name(&self) { - self.inner.$name.store(true, Ordering::Relaxed); + pub fn $name(&mut self) { + self.inner.$name = true; if !self.external { $( From d5683b7793fe2076014a01c0dd2d545d1442d2da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 23 Jul 2024 14:13:52 +0900 Subject: [PATCH 3/7] bool --- crates/swc_ecma_transforms_base/src/helpers/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/swc_ecma_transforms_base/src/helpers/mod.rs b/crates/swc_ecma_transforms_base/src/helpers/mod.rs index af78c0f4fa84..1ebcb227df97 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/mod.rs +++ b/crates/swc_ecma_transforms_base/src/helpers/mod.rs @@ -49,7 +49,7 @@ macro_rules! add_to { parse(&code) }); - let enable = $b.load(Ordering::Relaxed); + let enable = $b; if enable { $buf.extend(STMTS.iter().cloned().map(|mut stmt| { stmt.visit_mut_with(&mut Marker { @@ -66,7 +66,7 @@ macro_rules! add_to { macro_rules! add_import_to { ($buf:expr, $name:ident, $b:expr, $mark:expr) => {{ - let enable = $b.load(Ordering::Relaxed); + let enable = $b; if enable { let ctxt = SyntaxContext::empty().apply_mark($mark); let s = ImportSpecifier::Named(ImportNamedSpecifier { @@ -159,8 +159,8 @@ macro_rules! define_helpers { impl Helpers { pub fn extend_from(&self, other: &Self) { $( - if other.inner.$name.load(Ordering::SeqCst) { - self.inner.$name.store(true, Ordering::Relaxed); + if other.inner.$name { + self.inner.$name = true; } )* } @@ -171,7 +171,7 @@ macro_rules! define_helpers { HELPERS.with(|helpers|{ false $( - || helpers.inner.$name.load(Ordering::Relaxed) + || helpers.inner.$name )* }) } @@ -207,7 +207,7 @@ macro_rules! define_helpers { HELPERS.with(|helpers|{ debug_assert!(helpers.external); $( - let enable = helpers.inner.$name.load(Ordering::Relaxed); + let enable = helpers.inner.$name; if enable { buf.push(self.build_reqire(stringify!($name), helpers.mark.0)) } From c6490205fec8608ebff1b1d43db3871baf8e0164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 23 Jul 2024 14:21:35 +0900 Subject: [PATCH 4/7] type --- .../src/helpers/mod.rs | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/crates/swc_ecma_transforms_base/src/helpers/mod.rs b/crates/swc_ecma_transforms_base/src/helpers/mod.rs index 1ebcb227df97..e5e31a08374c 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/mod.rs +++ b/crates/swc_ecma_transforms_base/src/helpers/mod.rs @@ -1,4 +1,4 @@ -use std::mem::replace; +use std::{cell::RefCell, mem::replace}; use once_cell::sync::Lazy; use rustc_hash::FxHashMap; @@ -102,7 +102,7 @@ better_scoped_tls::scoped_tls!( pub struct Helpers { external: bool, mark: HelperMark, - inner: Inner, + inner: RefCell, } impl Helpers { @@ -144,8 +144,8 @@ macro_rules! define_helpers { impl Helpers { $( - pub fn $name(&mut self) { - self.inner.$name = true; + pub fn $name(&self) { + self.inner.borrow_mut().$name = true; if !self.external { $( @@ -156,11 +156,14 @@ macro_rules! define_helpers { )* } + impl Helpers { - pub fn extend_from(&self, other: &Self) { + pub fn extend_from(&mut self, other: &Self) { + let other = other.inner.borrow(); + let mut me = self.inner.borrow_mut(); $( - if other.inner.$name { - self.inner.$name = true; + if other.$name { + me.$name = true; } )* } @@ -170,8 +173,9 @@ macro_rules! define_helpers { fn is_helper_used(&self) -> bool{ HELPERS.with(|helpers|{ + let inner = helpers.inner.borrow(); false $( - || helpers.inner.$name + || inner.$name )* }) } @@ -181,8 +185,9 @@ macro_rules! define_helpers { HELPERS.with(|helpers|{ debug_assert!(!helpers.external); + let inner = helpers.inner.borrow(); $( - add_to!(buf, $name, helpers.inner.$name, helpers.mark.0); + add_to!(buf, $name, inner.$name, helpers.mark.0); )* }); @@ -193,9 +198,10 @@ macro_rules! define_helpers { let mut buf = Vec::new(); HELPERS.with(|helpers|{ + let inner = helpers.inner.borrow(); debug_assert!(helpers.external); $( - add_import_to!(buf, $name, helpers.inner.$name, helpers.mark.0); + add_import_to!(buf, $name, inner.$name, helpers.mark.0); )* }); @@ -206,8 +212,9 @@ macro_rules! define_helpers { let mut buf = Vec::new(); HELPERS.with(|helpers|{ debug_assert!(helpers.external); + let inner = helpers.inner.borrow(); $( - let enable = helpers.inner.$name; + let enable = inner.$name; if enable { buf.push(self.build_reqire(stringify!($name), helpers.mark.0)) } From f6b5de706fc3569fb38caa3fd528a27821a64531 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 23 Jul 2024 14:25:29 +0900 Subject: [PATCH 5/7] fix --- .../src/helpers/mod.rs | 25 ++++++++++++++++++- crates/swc_ecma_transforms_base/src/perf.rs | 15 ++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/crates/swc_ecma_transforms_base/src/helpers/mod.rs b/crates/swc_ecma_transforms_base/src/helpers/mod.rs index e5e31a08374c..24717ee1df80 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/mod.rs +++ b/crates/swc_ecma_transforms_base/src/helpers/mod.rs @@ -105,6 +105,13 @@ pub struct Helpers { inner: RefCell, } +#[derive(Debug, Clone, Copy)] +pub struct HelperData { + external: bool, + mark: HelperMark, + inner: Inner, +} + impl Helpers { pub fn new(external: bool) -> Self { Helpers { @@ -121,6 +128,22 @@ impl Helpers { pub const fn external(&self) -> bool { self.external } + + pub fn data(&self) -> HelperData { + HelperData { + inner: *self.inner.borrow(), + external: self.external, + mark: self.mark, + } + } + + pub fn from_data(data: HelperData) -> Self { + Helpers { + external: data.external, + mark: data.mark, + inner: RefCell::new(data.inner), + } + } } #[derive(Debug, Clone, Copy)] @@ -137,7 +160,7 @@ macro_rules! define_helpers { $( $name:ident : ( $( $dep:ident ),* ), )* } ) => { - #[derive(Debug,Default)] + #[derive(Debug,Default, Clone, Copy)] struct Inner { $( $name: bool, )* } diff --git a/crates/swc_ecma_transforms_base/src/perf.rs b/crates/swc_ecma_transforms_base/src/perf.rs index 30d6e68bc910..28079a55ea60 100644 --- a/crates/swc_ecma_transforms_base/src/perf.rs +++ b/crates/swc_ecma_transforms_base/src/perf.rs @@ -5,6 +5,7 @@ use swc_ecma_ast::*; pub use swc_ecma_utils::parallel::*; use swc_ecma_visit::{Fold, FoldWith, Visit, VisitMut, VisitMutWith, VisitWith}; +use crate::helpers::Helpers; #[cfg(feature = "concurrent")] use crate::helpers::HELPERS; @@ -52,14 +53,18 @@ where if nodes.len() >= threshold { GLOBALS.with(|globals| { HELPERS.with(|helpers| { + let helpers = helpers.data(); + HANDLER.with(|handler| { use rayon::prelude::*; let visitor = nodes .into_par_iter() .map(|node| { + let helpers = Helpers::from_data(helpers); + GLOBALS.set(globals, || { - HELPERS.set(helpers, || { + HELPERS.set(&helpers, || { HANDLER.set(handler, || { let mut visitor = Parallel::create(&*self); node.visit_with(&mut visitor); @@ -110,14 +115,16 @@ where if nodes.len() >= threshold { GLOBALS.with(|globals| { HELPERS.with(|helpers| { + let helpers = helpers.data(); HANDLER.with(|handler| { use rayon::prelude::*; let visitor = nodes .into_par_iter() .map(|node| { + let helpers = Helpers::from_data(helpers); GLOBALS.set(globals, || { - HELPERS.set(helpers, || { + HELPERS.set(&helpers, || { HANDLER.set(handler, || { let mut visitor = Parallel::create(&*self); node.visit_mut_with(&mut visitor); @@ -170,12 +177,14 @@ where let (visitor, nodes) = GLOBALS.with(|globals| { HELPERS.with(|helpers| { + let helpers = helpers.data(); HANDLER.with(|handler| { nodes .into_par_iter() .map(|node| { + let helpers = Helpers::from_data(helpers); GLOBALS.set(globals, || { - HELPERS.set(helpers, || { + HELPERS.set(&helpers, || { HANDLER.set(handler, || { let mut visitor = Parallel::create(&*self); let node = node.fold_with(&mut visitor); From 12763360e7e0d7d841215b7fff723b2b07f6f8a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 23 Jul 2024 14:29:21 +0900 Subject: [PATCH 6/7] fix bundler --- crates/swc_bundler/src/bundler/chunk/merge.rs | 14 +++++++++++++- crates/swc_bundler/src/bundler/finalize.rs | 9 +++++---- crates/swc_bundler/src/bundler/load.rs | 9 ++++++--- crates/swc_ecma_transforms_base/src/helpers/mod.rs | 2 +- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/crates/swc_bundler/src/bundler/chunk/merge.rs b/crates/swc_bundler/src/bundler/chunk/merge.rs index ff0d193533c4..6c0ef0a2a81b 100644 --- a/crates/swc_bundler/src/bundler/chunk/merge.rs +++ b/crates/swc_bundler/src/bundler/chunk/merge.rs @@ -9,6 +9,7 @@ use swc_common::{ FileName, SyntaxContext, DUMMY_SP, }; use swc_ecma_ast::*; +use swc_ecma_transforms_base::helpers::Helpers; use swc_ecma_utils::{find_pat_ids, prepend_stmt, private_ident, quote_ident, ExprFactory}; use swc_ecma_visit::{VisitMut, VisitMutWith}; use EdgeDirection::Outgoing; @@ -104,7 +105,18 @@ where let deps = all_deps_of_entry.iter().map(|id| { let dep_info = self.scope.get_module(*id).unwrap(); entry_info.helpers.extend(&dep_info.helpers); - entry_info.swc_helpers.extend_from(&dep_info.swc_helpers); + + { + let helpers = *entry_info.swc_helpers.lock(); + let dep_helpers = *dep_info.swc_helpers.lock(); + + let helpers = Helpers::from_data(helpers); + let dep_helpers = Helpers::from_data(dep_helpers); + + helpers.extend_from(&dep_helpers); + + *entry_info.swc_helpers.lock() = helpers.data(); + } if *id == entry_id { return Modules::empty(injected_ctxt); diff --git a/crates/swc_bundler/src/bundler/finalize.rs b/crates/swc_bundler/src/bundler/finalize.rs index 18eb71581703..58208ccd0117 100644 --- a/crates/swc_bundler/src/bundler/finalize.rs +++ b/crates/swc_bundler/src/bundler/finalize.rs @@ -6,7 +6,7 @@ use swc_common::{collections::AHashMap, util::move_map::MoveMap, FileName, Mark, use swc_ecma_ast::*; use swc_ecma_transforms_base::{ fixer::fixer, - helpers::{inject_helpers, HELPERS}, + helpers::{inject_helpers, Helpers, HELPERS}, hygiene::hygiene, }; use swc_ecma_utils::{contains_top_level_await, find_pat_ids, private_ident, ExprFactory}; @@ -48,15 +48,16 @@ where { // Inject swc helpers - let swc_helpers = self + let swc_helpers = *self .scope .get_module(bundle.id) .expect("module should exist at this point") - .swc_helpers; + .swc_helpers + .lock(); let module = bundle.module; - bundle.module = HELPERS.set(&swc_helpers, || { + bundle.module = HELPERS.set(&Helpers::from_data(swc_helpers), || { module.fold_with(&mut inject_helpers(unresolved_mark)) }); } diff --git a/crates/swc_bundler/src/bundler/load.rs b/crates/swc_bundler/src/bundler/load.rs index 374e7ecf0a23..0c543c2e1a4d 100644 --- a/crates/swc_bundler/src/bundler/load.rs +++ b/crates/swc_bundler/src/bundler/load.rs @@ -4,7 +4,10 @@ use anyhow::{Context, Error}; use is_macro::Is; #[cfg(feature = "rayon")] use rayon::iter::ParallelIterator; -use swc_common::{sync::Lrc, FileName, SourceFile, SyntaxContext}; +use swc_common::{ + sync::{Lock, Lrc}, + FileName, SourceFile, SyntaxContext, +}; use swc_ecma_ast::{ CallExpr, Callee, Expr, Ident, ImportDecl, ImportSpecifier, MemberExpr, MemberProp, Module, ModuleDecl, ModuleExportName, Str, SuperProp, SuperPropExpr, @@ -38,7 +41,7 @@ pub(crate) struct TransformedModule { /// Used helpers pub helpers: Lrc, - pub swc_helpers: Lrc, + pub swc_helpers: Lrc>, local_ctxt: SyntaxContext, export_ctxt: SyntaxContext, @@ -208,7 +211,7 @@ where exports: Lrc::new(exports), is_es6, helpers: Default::default(), - swc_helpers: Lrc::new(data.helpers), + swc_helpers: Lrc::new(Lock::new(data.helpers.data())), local_ctxt: SyntaxContext::empty().apply_mark(local_mark), export_ctxt: SyntaxContext::empty().apply_mark(export_mark), }, diff --git a/crates/swc_ecma_transforms_base/src/helpers/mod.rs b/crates/swc_ecma_transforms_base/src/helpers/mod.rs index 24717ee1df80..7720369d2ea7 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/mod.rs +++ b/crates/swc_ecma_transforms_base/src/helpers/mod.rs @@ -181,7 +181,7 @@ macro_rules! define_helpers { impl Helpers { - pub fn extend_from(&mut self, other: &Self) { + pub fn extend_from(&self, other: &Self) { let other = other.inner.borrow(); let mut me = self.inner.borrow_mut(); $( From d59e313e44d0667252f0c225cd8becdbb5794db9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 23 Jul 2024 14:30:56 +0900 Subject: [PATCH 7/7] Create cold-pillows-walk.md --- .changeset/cold-pillows-walk.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/cold-pillows-walk.md diff --git a/.changeset/cold-pillows-walk.md b/.changeset/cold-pillows-walk.md new file mode 100644 index 000000000000..4dfd392a0bb2 --- /dev/null +++ b/.changeset/cold-pillows-walk.md @@ -0,0 +1,6 @@ +--- +swc_bundler: patch +swc_ecma_transforms_base: patch +--- + +perf(es/helpers): Use `bool` instead of `AtomicBool`