From 11485e401eeb3e324e9e6506b9bc3a40937a6eb5 Mon Sep 17 00:00:00 2001 From: xusd320 Date: Wed, 7 Feb 2024 15:47:35 +0800 Subject: [PATCH] perf: use specific allocator on different platform --- Cargo.lock | 48 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 10 +++++---- crates/mako/Cargo.toml | 6 +++++ crates/mako/src/lib.rs | 12 ++++++++++ crates/mako/src/main.rs | 12 ++++++++++ crates/node/package.json | 3 ++- 6 files changed, 86 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fe9d07c33..dbf9b5bb7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1148,6 +1148,12 @@ dependencies = [ "syn 2.0.32", ] +[[package]] +name = "cty" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" + [[package]] name = "daachorse" version = "1.0.0" @@ -2539,11 +2545,13 @@ dependencies = [ "mako_core", "maplit", "miette 5.10.0", + "mimalloc-rust", "serde", "serde_json", "swc_core 0.83.19", "swc_ecma_transforms_testing", "testing 0.35.10", + "tikv-jemallocator", ] [[package]] @@ -2763,6 +2771,26 @@ dependencies = [ "syn 2.0.32", ] +[[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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6413e13241a9809f291568133eca6694572cf528c1a6175502d090adce5dd5db" +dependencies = [ + "cc", + "cty", +] + [[package]] name = "mime" version = "0.3.17" @@ -6232,6 +6260,26 @@ dependencies = [ "once_cell", ] +[[package]] +name = "tikv-jemalloc-sys" +version = "0.5.4+5.3.0-patched" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9402443cb8fd499b6f327e40565234ff34dbda27460c5b47db0db77443dd85d1" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "tikv-jemallocator" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "965fe0c26be5c56c94e38ba547249074803efd52adfb66de62107d95aab3eaca" +dependencies = [ + "libc", + "tikv-jemalloc-sys", +] + [[package]] name = "time" version = "0.3.22" diff --git a/Cargo.toml b/Cargo.toml index f06a0165d..c937b7086 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,9 @@ resolver = "2" members = ["crates/*"] [workspace.dependencies] -serde = { version = "1.0.171", features = ["derive"] } -serde_json = "1.0.100" -cached = "0.46.1" -swc_core = { version = "=0.83.19", default-features = false } +mimalloc-rust = { version = "=0.2.1" } +serde = { version = "1.0.171", features = ["derive"] } +serde_json = "1.0.100" +cached = "0.46.1" +swc_core = { version = "=0.83.19", default-features = false } +tikv-jemallocator = { version = "=0.5.4", features = ["disable_initial_exec_tls"] } diff --git a/crates/mako/Cargo.toml b/crates/mako/Cargo.toml index 83a63996a..7a3e4468f 100644 --- a/crates/mako/Cargo.toml +++ b/crates/mako/Cargo.toml @@ -19,6 +19,12 @@ swc_core = { workspace = true, features = ["swc_ecma_quote_macros"] } miette = { version = "5.10.0", features = ["fancy"] } glob-match = "0.2.1" +[target.'cfg(not(target_os = "linux"))'.dependencies] +mimalloc-rust = { workspace = true } + +[target.'cfg(all(target_os = "linux", target_env = "gnu", any(target_arch = "x86_64", target_arch = "aarch64")))'.dependencies] +tikv-jemallocator = { workspace = true } + [dev-dependencies] insta = { version = "1.30.0", features = ["yaml"] } maplit = "1.0.2" diff --git a/crates/mako/src/lib.rs b/crates/mako/src/lib.rs index c2d2f0e27..109ce497c 100644 --- a/crates/mako/src/lib.rs +++ b/crates/mako/src/lib.rs @@ -3,6 +3,18 @@ #![feature(let_chains)] #![feature(result_option_inspect)] +#[cfg(not(target_os = "linux"))] +#[global_allocator] +static GLOBAL: mimalloc_rust::GlobalMiMalloc = mimalloc_rust::GlobalMiMalloc; + +#[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; + mod analyze_deps; mod ast; mod build; diff --git a/crates/mako/src/main.rs b/crates/mako/src/main.rs index 65fec9798..184664980 100644 --- a/crates/mako/src/main.rs +++ b/crates/mako/src/main.rs @@ -59,6 +59,18 @@ mod update; mod util; mod watch; +#[cfg(not(target_os = "linux"))] +#[global_allocator] +static GLOBAL: mimalloc_rust::GlobalMiMalloc = mimalloc_rust::GlobalMiMalloc; + +#[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; + #[tokio::main] async fn main() -> Result<()> { // logger diff --git a/crates/node/package.json b/crates/node/package.json index 2c1932984..df3a3950f 100644 --- a/crates/node/package.json +++ b/crates/node/package.json @@ -42,5 +42,6 @@ "@okamjs/okam-darwin-arm64": "0.4.3", "@okamjs/okam-darwin-x64": "0.4.3", "@okamjs/okam-linux-x64-gnu": "0.4.3" - } + }, + "repository": "git@github.com:umijs/mako.git" } \ No newline at end of file