From 3b49824c7a7d614aa1c2faf7f7d725303dc8a6f8 Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 30 Jul 2024 15:02:09 +0800 Subject: [PATCH 1/4] perf: use mimalloc v2 for all --- .cargo/config.toml | 2 +- Cargo.lock | 58 ++++++++---------------------- Cargo.toml | 2 +- crates/node_binding/package.json | 1 + crates/rspack_allocator/Cargo.toml | 9 ++--- crates/rspack_allocator/src/lib.rs | 15 ++------ 6 files changed, 23 insertions(+), 64 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 278358cf0c0..7f6e8105529 100755 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -49,4 +49,4 @@ rustflags = ["-C", "link-args=/FORCE"] [target.x86_64-pc-windows-msvc] rustflags = ["-C", "target-feature=+crt-static"] [target.i686-pc-windows-msvc] -rustflags = ["-C", "target-feature=+crt-static"] +rustflags = ["-C", "target-feature=+crt-static"] \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index ed9733f9dc1..9d437c049ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -770,12 +770,6 @@ dependencies = [ "syn 2.0.63", ] -[[package]] -name = "cty" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" - [[package]] name = "darling" version = "0.14.4" @@ -1857,6 +1851,16 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "libmimalloc-sys" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23aa6811d3bd4deb8a84dde645f943476d13b248d818edcf8ce0b2f37f036b44" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "lightningcss" version = "1.0.0-alpha.57" @@ -2101,23 +2105,12 @@ dependencies = [ ] [[package]] -name = "mimalloc-rust" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eb726c8298efb4010b2c46d8050e4be36cf807b9d9e98cb112f830914fc9bbe" -dependencies = [ - "cty", - "mimalloc-rust-sys", -] - -[[package]] -name = "mimalloc-rust-sys" -version = "1.7.9-source" +name = "mimalloc" +version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6413e13241a9809f291568133eca6694572cf528c1a6175502d090adce5dd5db" +checksum = "68914350ae34959d83f732418d51e2427a794055d0b9529f48259ac07af65633" dependencies = [ - "cc", - "cty", + "libmimalloc-sys", ] [[package]] @@ -3019,8 +3012,7 @@ dependencies = [ name = "rspack_allocator" version = "0.1.0" dependencies = [ - "mimalloc-rust", - "tikv-jemallocator", + "mimalloc", ] [[package]] @@ -6116,26 +6108,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "tikv-jemalloc-sys" -version = "0.6.0+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd3c60906412afa9c2b5b5a48ca6a5abe5736aec9eb48ad05037a677e52e4e2d" -dependencies = [ - "cc", - "libc", -] - -[[package]] -name = "tikv-jemallocator" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cec5ff18518d81584f477e9bfdf957f5bb0979b0bac3af4ca30b5b3ae2d2865" -dependencies = [ - "libc", - "tikv-jemalloc-sys", -] - [[package]] name = "time" version = "0.3.34" diff --git a/Cargo.toml b/Cargo.toml index 386e3be9540..f2eeef588b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ itertools = { version = "0.13.0" } json = { version = "0.12.4" } lightningcss = { version = "1.0.0-alpha.57" } linked_hash_set = { version = "0.1.4" } -mimalloc-rust = { version = "0.2" } +mimalloc = { version = "0.1.43" } mime_guess = { version = "2.0.4" } once_cell = { version = "1.19.0" } parcel_sourcemap = "2.1.1" diff --git a/crates/node_binding/package.json b/crates/node_binding/package.json index 15638c995b2..26e05347a56 100644 --- a/crates/node_binding/package.json +++ b/crates/node_binding/package.json @@ -24,6 +24,7 @@ "build:release:arm64": "cross-env RUST_TARGET=aarch64-apple-darwin node scripts/build.js --release", "build:release:x64": "cross-env RUST_TARGET=x86_64-apple-darwin node scripts/build.js --release", "build:release:linux": "cross-env RUST_TARGET=x86_64-unknown-linux-gnu node scripts/build.js --release", + "build:release:musl": "cross-env RUST_TARGET=aarch64-unknown-linux-musl node scripts/build.js --release", "build:release:win": "cross-env RUST_TARGET=x86_64-pc-windows-msvc node scripts/build.js --release", "build:release-prod:all": "run-p build:release-prod:arm64 build:release-prod:x64 build:release-prod:linux && pnpm move-binding", "build:release-prod": "node scripts/build.js --release-prod", diff --git a/crates/rspack_allocator/Cargo.toml b/crates/rspack_allocator/Cargo.toml index 4114cb35f80..697b1ca5ae1 100644 --- a/crates/rspack_allocator/Cargo.toml +++ b/crates/rspack_allocator/Cargo.toml @@ -8,11 +8,8 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[target.'cfg(not(target_os = "linux"))'.dependencies] -mimalloc-rust = { workspace = true } - [target.'cfg(all(target_os = "linux", target_env = "musl"))'.dependencies] -mimalloc-rust = { workspace = true, features = ["local-dynamic-tls"] } +mimalloc = { workspace = true, features = ["local_dynamic_tls"] } -[target.'cfg(all(target_os = "linux", target_env = "gnu", any(target_arch = "x86_64", target_arch = "aarch64")))'.dependencies] -tikv-jemallocator = { workspace = true } +[target.'cfg(not(all(target_os = "linux", target_env = "musl")))'.dependencies] +mimalloc = { workspace = true } diff --git a/crates/rspack_allocator/src/lib.rs b/crates/rspack_allocator/src/lib.rs index 1e7ddea64cc..272a4081a1b 100644 --- a/crates/rspack_allocator/src/lib.rs +++ b/crates/rspack_allocator/src/lib.rs @@ -1,15 +1,4 @@ -#[cfg(not(target_os = "linux"))] -#[global_allocator] -static GLOBAL: mimalloc_rust::GlobalMiMalloc = mimalloc_rust::GlobalMiMalloc; - -#[cfg(all(target_os = "linux", target_env = "musl"))] -#[global_allocator] -static GLOBAL: mimalloc_rust::GlobalMiMalloc = mimalloc_rust::GlobalMiMalloc; +use mimalloc::MiMalloc; -#[cfg(all( - target_os = "linux", - target_env = "gnu", - any(target_arch = "x86_64", target_arch = "aarch64") -))] #[global_allocator] -static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; +static GLOBAL: MiMalloc = MiMalloc; From 3f1957303b5e18dfbc5c1a58b730f53a427dec70 Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 30 Jul 2024 15:06:42 +0800 Subject: [PATCH 2/4] chore: remove dep --- Cargo.toml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f2eeef588b0..bad504cf93e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,10 +55,9 @@ ustr = { package = "ustr-fxhash", version = "1.0.0" } xxhash-rust = { version = "0.8.10" } # Pinned -napi = { package = "napi-h", version = "=2.16.1" } -napi-build = { version = "2" } -napi-derive = { version = "2" } -tikv-jemallocator = { version = "=0.6.0", features = ["disable_initial_exec_tls"] } +napi = { package = "napi-h", version = "=2.16.1" } +napi-build = { version = "2" } +napi-derive = { version = "2" } # Must be pinned with the same swc versions #rkyv = { version = "=0.7.44" } # synced with swc wasm plugin From 6cf4792d2e180c259ec66c1273cea22e24552e05 Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 30 Jul 2024 15:41:02 +0800 Subject: [PATCH 3/4] fix: fix initial tls on linux --- crates/rspack_allocator/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/rspack_allocator/Cargo.toml b/crates/rspack_allocator/Cargo.toml index 697b1ca5ae1..a393d588ee3 100644 --- a/crates/rspack_allocator/Cargo.toml +++ b/crates/rspack_allocator/Cargo.toml @@ -7,9 +7,9 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[target.'cfg(all(target_os = "linux", target_env = "musl"))'.dependencies] +[target.'cfg(target_os = "linux")'.dependencies] +# Turned on `local_dynamic_tls` to avoid issue: https://github.com/microsoft/mimalloc/issues/147 mimalloc = { workspace = true, features = ["local_dynamic_tls"] } -[target.'cfg(not(all(target_os = "linux", target_env = "musl")))'.dependencies] +[target.'cfg(not(target_os = "linux"))'.dependencies] mimalloc = { workspace = true } From 7550b7f0be7271b69d65e964042db89a552ed4ea Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 30 Jul 2024 17:38:47 +0800 Subject: [PATCH 4/4] ci: aarch64 linux gnu build --- .github/workflows/reusable-build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/reusable-build.yml b/.github/workflows/reusable-build.yml index 781e57adf1f..b5229d45924 100644 --- a/.github/workflows/reusable-build.yml +++ b/.github/workflows/reusable-build.yml @@ -142,7 +142,7 @@ jobs: target: ${{ inputs.target }} image: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian-aarch64 profile: ${{ inputs.profile }} - pre: export JEMALLOC_SYS_WITH_LG_PAGE=16 && export CC_aarch64_unknown_linux_gnu=/usr/aarch64-unknown-linux-gnu/bin/aarch64-unknown-linux-gnu-gcc # for jemallocator to compile + pre: export CC_aarch64_unknown_linux_gnu=clang - name: Build x86_64-unknown-linux-musl in Docker if: ${{ inputs.target == 'x86_64-unknown-linux-musl' && steps.check_cache.outputs.files_exists != 'true' && !inputs.skipable }}