diff --git a/Cargo.lock b/Cargo.lock index 05d751cb..27f7fb1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -437,6 +437,12 @@ dependencies = [ "typenum", ] +[[package]] +name = "cty" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" + [[package]] name = "dashmap" version = "5.5.3" @@ -1080,6 +1086,26 @@ dependencies = [ "syn", ] +[[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" @@ -1309,6 +1335,7 @@ dependencies = [ "home", "insta", "miette", + "num_cpus", "pacquet-diagnostics", "pacquet-executor", "pacquet-fs", @@ -1322,8 +1349,10 @@ dependencies = [ "pacquet-testing-utils", "pipe-trait", "pretty_assertions", + "rayon", "reqwest", "serde_json", + "swc_malloc", "tempfile", "tokio", "walkdir", @@ -2154,6 +2183,16 @@ dependencies = [ "is-terminal", ] +[[package]] +name = "swc_malloc" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a279493814466a779ac93921b8a88fbd9f9162807e564d64dbbae2edee00c8a" +dependencies = [ + "mimalloc-rust", + "tikv-jemallocator", +] + [[package]] name = "syn" version = "2.0.28" @@ -2252,6 +2291,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 = "tinytemplate" version = "1.2.1" diff --git a/Cargo.toml b/Cargo.toml index 2b2109f7..373bfa95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ reflink-copy = { version = "0.1.9" } junction = { version = "1.0.0" } reqwest = { version = "0.11", default-features = false, features = ["json", "native-tls-vendored"] } node-semver = { version = "2.1.0" } +num_cpus = { version = "1.16.0" } pipe-trait = { version = "0.4.0" } rayon = { version = "1.8.0" } serde = { version = "1.0.188", features = ["derive"] } @@ -54,6 +55,7 @@ sha2 = { version = "0.10.8" } split-first-char = { version = "0.0.0" } ssri = { version = "9.0.0" } strum = { version = "0.25.0", features = ["derive"] } +swc_malloc = { version = "0.5.9" } tar = { version = "0.4.40" } text-block-macros = { version = "0.1.1" } tracing = { version = "0.1.37" } @@ -78,9 +80,9 @@ allow_branch = "main" opt-level = 3 lto = "fat" codegen-units = 1 -strip = "symbols" -debug = false -panic = "abort" # Let it crash and force ourselves to write safe Rust. +# strip = "symbols" +# debug = false +panic = "abort" # Let it crash and force ourselves to write safe Rust. # Use the `--profile release-debug` flag to show symbols in release mode. # e.g. `cargo build --profile release-debug` diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index a5daff4e..126d49ea 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -32,6 +32,9 @@ miette = { workspace = true } reqwest = { workspace = true } pipe-trait = { workspace = true } tokio = { workspace = true } +swc_malloc = { workspace = true } +rayon = { workspace = true } +num_cpus = { workspace = true } [dev-dependencies] pacquet-store-dir = { workspace = true } diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 67989365..a858e548 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -1,3 +1,5 @@ +extern crate swc_malloc; + mod cli_args; mod state; @@ -8,6 +10,16 @@ use pacquet_diagnostics::enable_tracing_by_env; use state::State; pub async fn main() -> miette::Result<()> { + // We use rayon only for blocking syscalls, so we multiply the number of threads by 3. + // + // If we are going to use rayon for CPU-bound tasks, + // we should create an extra threadpool for IO-bound tasks, + // and use the global theadpool for CPU-bound tasks. + rayon::ThreadPoolBuilder::new() + .num_threads(num_cpus::get() * 3) + .build_global() + .expect("build rayon thread pool"); + enable_tracing_by_env(); set_panic_hook(); CliArgs::parse().run().await