Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(es/codegen): Optimize using swc_allocator #9294

Merged
merged 19 commits into from
Jul 19, 2024
6 changes: 6 additions & 0 deletions .changeset/four-cheetahs-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_allocator: patch
swc_ecma_codegen: patch
---

perf(es/codegen): Optimize using `swc_allocator`
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ pkg/

# Used by scripts
*.tmp.txt
swc-core-*.tgz
swc-core-*.tgz

# samply
profile.json
17 changes: 11 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion crates/swc_allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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 }

Expand Down
33 changes: 33 additions & 0 deletions crates/swc_allocator/src/collections.rs
Original file line number Diff line number Diff line change
@@ -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<K, V, S> = hashbrown::HashMap<K, V, S, FastAlloc>;

/// See [std::collections::HashSet].
#[cfg(feature = "nightly")]
pub type HashSet<T, S> = hashbrown::HashSet<T, S, FastAlloc>;

/// See [std::collections::HashMap].
#[cfg(not(feature = "nightly"))]
pub type HashMap<K, V, S> = hashbrown::HashMap<K, V, S>;

#[cfg(not(feature = "nightly"))]
/// See [std::collections::HashSet].
pub type HashSet<T, S> = hashbrown::HashSet<T, S>;

/// Used for `FxHashMap` and `FxHashSet`.
pub type FxBuildHasher = BuildHasherDefault<FxHasher>;

/// Faster `HashMap` which uses `FxHasher`.
pub type FxHashMap<K, V> = HashMap<K, V, FxBuildHasher>;

/// Faster `HashSet` which uses `FxHasher`.
pub type FxHashSet<T> = HashSet<T, FxBuildHasher>;
1 change: 1 addition & 0 deletions crates/swc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion crates/swc_ecma_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_ecma_codegen/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct Builder {
}

impl Builder {
pub fn with<'a, F, Ret>(self, _: &str, s: &'a mut Vec<u8>, op: F) -> Ret
pub fn with<'a, F, Ret>(self, _: &str, s: &'a mut std::vec::Vec<u8>, op: F) -> Ret
where
F: for<'aa> FnOnce(&mut Emitter<'aa, Box<(dyn WriteJs + 'aa)>, SourceMap>) -> Ret,
Ret: 'static,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions crates/swc_ecma_codegen/src/text_writer/basic_impl.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
3 changes: 1 addition & 2 deletions crates/swc_ecma_codegen/tests/sourcemap.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
Loading