From c2860e20c51503050d5b9c6d8745735f526c71a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:03:27 +0900 Subject: [PATCH 01/18] gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 705f9921b795..5e0044ae1a8a 100644 --- a/.gitignore +++ b/.gitignore @@ -61,4 +61,7 @@ pkg/ # Used by scripts *.tmp.txt -swc-core-*.tgz \ No newline at end of file +swc-core-*.tgz + +# samply +profile.json \ No newline at end of file From 84574e740d228f8a86754c3db79d888e24b61a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:03:55 +0900 Subject: [PATCH 02/18] vec --- crates/swc_ecma_codegen/src/lib.rs | 3 ++- crates/swc_ecma_codegen/src/tests.rs | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index 3a3e1f3dc093..6fb1b8a37801 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -9,6 +9,7 @@ use std::{borrow::Cow, fmt::Write, io}; use memchr::memmem::Finder; use once_cell::sync::Lazy; +use swc_allocator::maybe::vec::Vec; use swc_atoms::Atom; use swc_common::{ comments::{CommentKind, Comments}, @@ -44,7 +45,7 @@ pub fn to_code_default( comments: Option<&dyn Comments>, node: &impl Node, ) -> String { - let mut buf = Vec::new(); + let mut buf = std::vec::Vec::new(); { let mut emitter = Emitter { cfg: Default::default(), diff --git a/crates/swc_ecma_codegen/src/tests.rs b/crates/swc_ecma_codegen/src/tests.rs index e5486a471c2e..416a4732cbd0 100644 --- a/crates/swc_ecma_codegen/src/tests.rs +++ b/crates/swc_ecma_codegen/src/tests.rs @@ -16,7 +16,7 @@ struct Builder { } impl Builder { - pub fn with<'a, F, Ret>(self, _: &str, s: &'a mut Vec, op: F) -> Ret + pub fn with<'a, F, Ret>(self, _: &str, s: &'a mut std::vec::Vec, op: F) -> Ret where F: for<'aa> FnOnce(&mut Emitter<'aa, Box<(dyn WriteJs + 'aa)>, SourceMap>) -> Ret, Ret: 'static, @@ -44,7 +44,7 @@ impl Builder { where F: for<'aa> FnOnce(&mut Emitter<'aa, Box<(dyn WriteJs + 'aa)>, SourceMap>), { - let mut buf = Vec::new(); + let mut buf = std::vec::Vec::new(); self.with(src, &mut buf, op); @@ -974,7 +974,7 @@ fn run_node(code: &str) -> String { JsExecOptions { cache: true, module: false, - args: Vec::new(), + args: Default::default(), }, ) .expect("failed to execute node.js") From 9899e12ccda65ea8c900c538402bf270bd74603e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:22:05 +0900 Subject: [PATCH 03/18] Dep on hashbrown --- Cargo.toml | 1 + crates/swc_allocator/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index c2a4568a835e..dbd7c489aac8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,6 +75,7 @@ resolver = "2" flate2 = "1.0" futures = "0.3" glob = "0.3.0" + hashbrown = "0.14.5" hex = "0.4.3" hstr = "0.2.8" indexmap = "2.0.0" diff --git a/crates/swc_allocator/Cargo.toml b/crates/swc_allocator/Cargo.toml index 26b7acc8b21d..fe92db7a5e0c 100644 --- a/crates/swc_allocator/Cargo.toml +++ b/crates/swc_allocator/Cargo.toml @@ -26,6 +26,7 @@ bumpalo = { workspace = true, features = [ "collections", "allocator-api2", ] } +hashbrown = { workspace = true } ptr_meta = { workspace = true } rkyv = { workspace = true, optional = true } serde = { workspace = true, optional = true } From c6d566d2c1c84f9018d51f9b246ae40c6f682a21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:22:11 +0900 Subject: [PATCH 04/18] cargo lockfile --- Cargo.lock | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6627742d3109..4318132de5c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1073,7 +1073,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" dependencies = [ "cfg-if", - "hashbrown 0.14.3", + "hashbrown 0.14.5", "lock_api", "once_cell", "parking_lot_core", @@ -1635,9 +1635,13 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.14.3" +version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash 0.8.8", + "allocator-api2", +] [[package]] name = "heapless" @@ -1700,7 +1704,7 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0f5356d62012374578cd3a5c013d6586de3efbca3b53379fc1edfbb95c9db14" dependencies = [ - "hashbrown 0.14.3", + "hashbrown 0.14.5", "new_debug_unreachable", "once_cell", "phf", @@ -1864,7 +1868,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ "equivalent", - "hashbrown 0.14.3", + "hashbrown 0.14.5", "rayon", "serde", ] @@ -3726,6 +3730,7 @@ dependencies = [ "bumpalo", "codspeed-criterion-compat", "criterion", + "hashbrown 0.14.5", "ptr_meta", "rkyv", "serde", From 53e0e984ee7a73ba3759ae7d081f18dfeba82c1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:26:04 +0900 Subject: [PATCH 05/18] hashset --- crates/swc_allocator/src/collections.rs | 8 ++++++++ crates/swc_allocator/src/lib.rs | 1 + 2 files changed, 9 insertions(+) create mode 100644 crates/swc_allocator/src/collections.rs diff --git a/crates/swc_allocator/src/collections.rs b/crates/swc_allocator/src/collections.rs new file mode 100644 index 000000000000..d831fb177d48 --- /dev/null +++ b/crates/swc_allocator/src/collections.rs @@ -0,0 +1,8 @@ +//! Various collections. + +use crate::FastAlloc; + +/// See [std::collections::HashMap]. +pub type HashMap = hashbrown::HashMap; +/// See [std::collections::HashSet]. +pub type HashSet = hashbrown::HashSet; diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 2a39dd4556f3..1ca1f9deb4a5 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -39,6 +39,7 @@ pub use crate::alloc::Allocator; mod alloc; #[cfg(feature = "nightly")] pub mod boxed; +pub mod collections; #[cfg(feature = "nightly")] pub mod vec; From eac46f4f8f22bbf10eea04b73eb38e0c61d2554b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:28:03 +0900 Subject: [PATCH 06/18] cargo feature --- crates/swc_allocator/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_allocator/Cargo.toml b/crates/swc_allocator/Cargo.toml index fe92db7a5e0c..af6318e12ad4 100644 --- a/crates/swc_allocator/Cargo.toml +++ b/crates/swc_allocator/Cargo.toml @@ -15,7 +15,7 @@ version = "0.1.7" [features] default = ["scoped"] -nightly = ["bumpalo/allocator_api"] +nightly = ["bumpalo/allocator_api", "hashbrown/nightly"] rkyv = ["dep:rkyv"] scoped = ["nightly"] serde = ["dep:serde"] From 700c35dc7a7669ced7bef1c0165964db658cf796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:29:44 +0900 Subject: [PATCH 07/18] dep --- crates/swc_allocator/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/swc_allocator/Cargo.toml b/crates/swc_allocator/Cargo.toml index af6318e12ad4..0f23fc4382a4 100644 --- a/crates/swc_allocator/Cargo.toml +++ b/crates/swc_allocator/Cargo.toml @@ -29,6 +29,7 @@ bumpalo = { workspace = true, features = [ hashbrown = { workspace = true } ptr_meta = { workspace = true } rkyv = { workspace = true, optional = true } +rustc-hash = { workspace = true } serde = { workspace = true, optional = true } triomphe = { workspace = true } From cdcee3e332cadbd52e0e1b528bacb375d62e0cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:29:50 +0900 Subject: [PATCH 08/18] cargo lockifle --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index 4318132de5c1..9b377ed7171f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3733,6 +3733,7 @@ dependencies = [ "hashbrown 0.14.5", "ptr_meta", "rkyv", + "rustc-hash", "serde", "swc_malloc", "triomphe", From cabced7c5bc48c8232df90980ae607d26f9dafb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:31:02 +0900 Subject: [PATCH 09/18] collections --- crates/swc_allocator/src/collections.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/swc_allocator/src/collections.rs b/crates/swc_allocator/src/collections.rs index d831fb177d48..373803823d75 100644 --- a/crates/swc_allocator/src/collections.rs +++ b/crates/swc_allocator/src/collections.rs @@ -1,8 +1,22 @@ //! Various collections. +use std::hash::BuildHasherDefault; + +use rustc_hash::FxHasher; + use crate::FastAlloc; /// See [std::collections::HashMap]. pub type HashMap = hashbrown::HashMap; + /// See [std::collections::HashSet]. pub type HashSet = hashbrown::HashSet; + +/// Used for `FxHashMap` and `FxHashSet`. +pub type FxBuildHasher = BuildHasherDefault; + +/// Faster `HashMap` which uses `FxHasher`. +pub type FxHashMap = HashMap; + +/// Faster `HashSet` which uses `FxHasher`. +pub type FxHashSet = HashSet; From 80212c5731984ff8d9bda9a04f90552c0563848c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:32:28 +0900 Subject: [PATCH 10/18] Improve collections --- crates/swc_allocator/src/collections.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/swc_allocator/src/collections.rs b/crates/swc_allocator/src/collections.rs index 373803823d75..2a83e55b2486 100644 --- a/crates/swc_allocator/src/collections.rs +++ b/crates/swc_allocator/src/collections.rs @@ -4,14 +4,25 @@ use std::hash::BuildHasherDefault; use rustc_hash::FxHasher; +#[cfg(feature = "nightly")] use crate::FastAlloc; /// See [std::collections::HashMap]. +#[cfg(feature = "nightly")] pub type HashMap = hashbrown::HashMap; /// See [std::collections::HashSet]. +#[cfg(feature = "nightly")] pub type HashSet = hashbrown::HashSet; +/// See [std::collections::HashMap]. +#[cfg(not(feature = "nightly"))] +pub type HashMap = hashbrown::HashMap; + +#[cfg(not(feature = "nightly"))] +/// See [std::collections::HashSet]. +pub type HashSet = hashbrown::HashSet; + /// Used for `FxHashMap` and `FxHashSet`. pub type FxBuildHasher = BuildHasherDefault; From 2951224e31541e644663c1bc281369c8fb837e1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:32:36 +0900 Subject: [PATCH 11/18] Drop dep --- crates/swc_ecma_codegen/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/swc_ecma_codegen/Cargo.toml b/crates/swc_ecma_codegen/Cargo.toml index f0c9db97d933..e50ca8024d0a 100644 --- a/crates/swc_ecma_codegen/Cargo.toml +++ b/crates/swc_ecma_codegen/Cargo.toml @@ -20,7 +20,6 @@ bench = false memchr = { workspace = true } num-bigint = { workspace = true, features = ["serde"] } once_cell = { workspace = true } -rustc-hash = { workspace = true } serde = { workspace = true } sourcemap = { workspace = true } tracing = { workspace = true } From 842c66d36411a3b2d655967efed2ef318468d9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:32:43 +0900 Subject: [PATCH 12/18] cargo lockfile --- Cargo.lock | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 9b377ed7171f..6d7422dd248b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4189,7 +4189,6 @@ dependencies = [ "memchr", "num-bigint", "once_cell", - "rustc-hash", "serde", "serde_json", "sourcemap", From 0efc325e8d63c6e667eacc21732133c8865c14c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:32:47 +0900 Subject: [PATCH 13/18] use it --- crates/swc_ecma_codegen/src/text_writer/basic_impl.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/swc_ecma_codegen/src/text_writer/basic_impl.rs b/crates/swc_ecma_codegen/src/text_writer/basic_impl.rs index 2da27b267470..43064643b251 100644 --- a/crates/swc_ecma_codegen/src/text_writer/basic_impl.rs +++ b/crates/swc_ecma_codegen/src/text_writer/basic_impl.rs @@ -1,7 +1,6 @@ use std::io::Write; -use rustc_hash::FxHashSet; -use swc_allocator::maybe::vec::Vec; +use swc_allocator::{collections::FxHashSet, maybe::vec::Vec}; use swc_common::{sync::Lrc, BytePos, LineCol, SourceMap, Span}; use super::{Result, WriteJs}; From 5e7a190d2314822ac0119e7bc83d554b43636d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 20 Jul 2024 02:35:42 +0900 Subject: [PATCH 14/18] Create four-cheetahs-type.md --- .changeset/four-cheetahs-type.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/four-cheetahs-type.md diff --git a/.changeset/four-cheetahs-type.md b/.changeset/four-cheetahs-type.md new file mode 100644 index 000000000000..ea7f7f19514c --- /dev/null +++ b/.changeset/four-cheetahs-type.md @@ -0,0 +1,5 @@ +--- +swc_ecma_codegen: patch +--- + +perf(es/codegen): Optimize using `swc_allocator` From e85d6f9e8ddd0f78a12700dc7bef9359993329bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:48:05 +0900 Subject: [PATCH 15/18] fix build --- crates/swc_ecma_codegen/tests/sourcemap.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/swc_ecma_codegen/tests/sourcemap.rs b/crates/swc_ecma_codegen/tests/sourcemap.rs index ec8c9c1a4208..574813568c5f 100644 --- a/crates/swc_ecma_codegen/tests/sourcemap.rs +++ b/crates/swc_ecma_codegen/tests/sourcemap.rs @@ -1,9 +1,8 @@ use std::{fs::read_to_string, path::PathBuf}; use base64::prelude::{Engine, BASE64_STANDARD}; -use rustc_hash::FxHashSet; use sourcemap::SourceMap; -use swc_allocator::maybe::vec::Vec; +use swc_allocator::{collections::FxHashSet, maybe::vec::Vec}; use swc_common::{comments::SingleThreadedComments, source_map::SourceMapGenConfig}; use swc_ecma_ast::EsVersion; use swc_ecma_codegen::{text_writer::WriteJs, Emitter}; From 60c6297edb97fa7997ad66a9971e302443477ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:49:25 +0900 Subject: [PATCH 16/18] cap --- crates/swc_ecma_codegen/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index 6fb1b8a37801..09a708429dc5 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -4082,7 +4082,7 @@ fn get_ascii_only_ident(sym: &str, may_need_quote: bool, target: EsVersion) -> C } fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> String { - let mut buf = String::with_capacity(v.len() + 2); + let mut buf = String::with_capacity(v.len() * 12 / 10); let mut iter = v.chars().peekable(); let mut single_quote_count = 0; From 7ee7b1487b74bca715d5270ad629e4d5584c2d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 20 Jul 2024 02:51:23 +0900 Subject: [PATCH 17/18] Revert "cap" This reverts commit 60c6297edb97fa7997ad66a9971e302443477ef5. --- crates/swc_ecma_codegen/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index 09a708429dc5..6fb1b8a37801 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -4082,7 +4082,7 @@ fn get_ascii_only_ident(sym: &str, may_need_quote: bool, target: EsVersion) -> C } fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> String { - let mut buf = String::with_capacity(v.len() * 12 / 10); + let mut buf = String::with_capacity(v.len() + 2); let mut iter = v.chars().peekable(); let mut single_quote_count = 0; From ca70ec43151611e397f2a5d49fd162a519b5dcdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 20 Jul 2024 02:52:25 +0900 Subject: [PATCH 18/18] Update four-cheetahs-type.md --- .changeset/four-cheetahs-type.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/four-cheetahs-type.md b/.changeset/four-cheetahs-type.md index ea7f7f19514c..90c0a0244261 100644 --- a/.changeset/four-cheetahs-type.md +++ b/.changeset/four-cheetahs-type.md @@ -1,4 +1,5 @@ --- +swc_allocator: patch swc_ecma_codegen: patch ---