diff --git a/.changeset/four-cheetahs-type.md b/.changeset/four-cheetahs-type.md new file mode 100644 index 000000000000..90c0a0244261 --- /dev/null +++ b/.changeset/four-cheetahs-type.md @@ -0,0 +1,6 @@ +--- +swc_allocator: patch +swc_ecma_codegen: patch +--- + +perf(es/codegen): Optimize using `swc_allocator` 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 diff --git a/Cargo.lock b/Cargo.lock index 6627742d3109..6d7422dd248b 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,8 +3730,10 @@ dependencies = [ "bumpalo", "codspeed-criterion-compat", "criterion", + "hashbrown 0.14.5", "ptr_meta", "rkyv", + "rustc-hash", "serde", "swc_malloc", "triomphe", @@ -4183,7 +4189,6 @@ dependencies = [ "memchr", "num-bigint", "once_cell", - "rustc-hash", "serde", "serde_json", "sourcemap", 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..0f23fc4382a4 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"] @@ -26,8 +26,10 @@ bumpalo = { workspace = true, features = [ "collections", "allocator-api2", ] } +hashbrown = { workspace = true } ptr_meta = { workspace = true } rkyv = { workspace = true, optional = true } +rustc-hash = { workspace = true } serde = { workspace = true, optional = true } triomphe = { workspace = true } diff --git a/crates/swc_allocator/src/collections.rs b/crates/swc_allocator/src/collections.rs new file mode 100644 index 000000000000..2a83e55b2486 --- /dev/null +++ b/crates/swc_allocator/src/collections.rs @@ -0,0 +1,33 @@ +//! Various collections. + +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; + +/// Faster `HashMap` which uses `FxHasher`. +pub type FxHashMap = HashMap; + +/// Faster `HashSet` which uses `FxHasher`. +pub type FxHashSet = 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; 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 } 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") 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}; 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};