From d0b21dcda461a30f8e5ac383715d4720d8023368 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Fri, 10 Nov 2023 13:09:51 -0500 Subject: [PATCH] puffin-{cli,dev}: switch to mimalloc and jemalloc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This copies the allocator configuration used in the Ruff project. In particular, this gives us an instant 10% win when resolving the top 1K PyPI packages: $ hyperfine \ "./target/profiling/puffin-dev-main resolve-many --cache-dir cache-docker-no-build --no-build pypi_top_8k_flat.txt --limit 1000 2> /dev/null" \ "./target/profiling/puffin-dev resolve-many --cache-dir cache-docker-no-build --no-build pypi_top_8k_flat.txt --limit 1000 2> /dev/null" Benchmark 1: ./target/profiling/puffin-dev-main resolve-many --cache-dir cache-docker-no-build --no-build pypi_top_8k_flat.txt --limit 1000 2> /dev/null Time (mean ± σ): 974.2 ms ± 26.4 ms [User: 17503.3 ms, System: 2205.3 ms] Range (min … max): 943.5 ms … 1015.9 ms 10 runs Benchmark 2: ./target/profiling/puffin-dev resolve-many --cache-dir cache-docker-no-build --no-build pypi_top_8k_flat.txt --limit 1000 2> /dev/null Time (mean ± σ): 883.1 ms ± 23.3 ms [User: 14626.1 ms, System: 2542.2 ms] Range (min … max): 849.5 ms … 916.9 ms 10 runs Summary './target/profiling/puffin-dev resolve-many --cache-dir cache-docker-no-build --no-build pypi_top_8k_flat.txt --limit 1000 2> /dev/null' ran 1.10 ± 0.04 times faster than './target/profiling/puffin-dev-main resolve-many --cache-dir cache-docker-no-build --no-build pypi_top_8k_flat.txt --limit 1000 2> /dev/null' I was moved to do this because I noticed `malloc`/`free` taking up a fairly sizeable percentage of time during light profiling. --- Cargo.lock | 43 +++++++++++++++++++++++++++++++++++ crates/puffin-cli/Cargo.toml | 6 +++++ crates/puffin-cli/src/main.rs | 16 +++++++++++++ crates/puffin-dev/Cargo.toml | 6 +++++ crates/puffin-dev/src/main.rs | 16 +++++++++++++ 5 files changed, 87 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index c8a59fbb4..ab2f6dc5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1646,6 +1646,16 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "libmimalloc-sys" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3979b5c37ece694f1f5e51e7ecc871fdb0f517ed04ee45f88d15d6d553cb9664" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "libredox" version = "0.0.1" @@ -1805,6 +1815,15 @@ dependencies = [ "syn 2.0.39", ] +[[package]] +name = "mimalloc" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa01922b5ea280a911e323e4d2fd24b7fe5cc4042e0d2cda3c40775cdc4bdc9c" +dependencies = [ + "libmimalloc-sys", +] + [[package]] name = "mime" version = "0.3.17" @@ -2332,6 +2351,7 @@ dependencies = [ "install-wheel-rs", "itertools 0.11.0", "miette", + "mimalloc", "pep440_rs 0.3.12", "pep508_rs", "platform-host", @@ -2351,6 +2371,7 @@ dependencies = [ "requirements-txt", "tempfile", "thiserror", + "tikv-jemallocator", "tokio", "toml 0.8.8", "tracing", @@ -2403,6 +2424,7 @@ dependencies = [ "gourgeist", "indicatif", "itertools 0.11.0", + "mimalloc", "pep508_rs", "platform-host", "platform-tags", @@ -2413,6 +2435,7 @@ dependencies = [ "puffin-traits", "pypi-types", "tempfile", + "tikv-jemallocator", "tokio", "tracing", "tracing-indicatif", @@ -3535,6 +3558,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.30" diff --git a/crates/puffin-cli/Cargo.toml b/crates/puffin-cli/Cargo.toml index a4e7ab46d..29795f32d 100644 --- a/crates/puffin-cli/Cargo.toml +++ b/crates/puffin-cli/Cargo.toml @@ -54,6 +54,12 @@ tracing-tree = { workspace = true } url = { workspace = true } which = { workspace = true } +[target.'cfg(target_os = "windows")'.dependencies] +mimalloc = "0.1.39" + +[target.'cfg(all(not(target_os = "windows"), not(target_os = "openbsd"), any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "powerpc64")))'.dependencies] +tikv-jemallocator = "0.5.0" + [dev-dependencies] assert_cmd = { version = "2.0.12" } assert_fs = { version = "1.0.13" } diff --git a/crates/puffin-cli/src/main.rs b/crates/puffin-cli/src/main.rs index b2833bb85..6261c209e 100644 --- a/crates/puffin-cli/src/main.rs +++ b/crates/puffin-cli/src/main.rs @@ -18,6 +18,22 @@ use crate::index_urls::IndexUrls; use crate::python_version::PythonVersion; use crate::requirements::RequirementsSource; +#[cfg(target_os = "windows")] +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + +#[cfg(all( + not(target_os = "windows"), + not(target_os = "openbsd"), + any( + target_arch = "x86_64", + target_arch = "aarch64", + target_arch = "powerpc64" + ) +))] +#[global_allocator] +static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + mod commands; mod index_urls; mod logging; diff --git a/crates/puffin-dev/Cargo.toml b/crates/puffin-dev/Cargo.toml index bbb4261e5..0c99d4f67 100644 --- a/crates/puffin-dev/Cargo.toml +++ b/crates/puffin-dev/Cargo.toml @@ -38,3 +38,9 @@ tracing-indicatif = { workspace = true } tracing-subscriber = { workspace = true } which = { workspace = true } url = { workspace = true } + +[target.'cfg(target_os = "windows")'.dependencies] +mimalloc = "0.1.39" + +[target.'cfg(all(not(target_os = "windows"), not(target_os = "openbsd"), any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "powerpc64")))'.dependencies] +tikv-jemallocator = "0.5.0" diff --git a/crates/puffin-dev/src/main.rs b/crates/puffin-dev/src/main.rs index dafe745e8..a701a86c7 100644 --- a/crates/puffin-dev/src/main.rs +++ b/crates/puffin-dev/src/main.rs @@ -18,6 +18,22 @@ use crate::build::{build, BuildArgs}; use crate::resolve_cli::ResolveCliArgs; use crate::wheel_metadata::WheelMetadataArgs; +#[cfg(target_os = "windows")] +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + +#[cfg(all( + not(target_os = "windows"), + not(target_os = "openbsd"), + any( + target_arch = "x86_64", + target_arch = "aarch64", + target_arch = "powerpc64" + ) +))] +#[global_allocator] +static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + mod build; mod resolve_cli; mod resolve_many;