diff --git a/BUILD.bazel b/BUILD.bazel index 8ee5c2451..1335167aa 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -38,7 +38,12 @@ rust_binary( "@crates//:tower", "@crates//:tracing", "@crates//:tracing-subscriber", - ], + ] + select({ + "@platforms//os:windows": [], + "//conditions:default": [ + "@crates//:tikv-jemallocator", + ], + }), ) genrule( diff --git a/Cargo.lock b/Cargo.lock index ff8d4ccc0..ff7d8e200 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1682,6 +1682,7 @@ dependencies = [ "rustls-pemfile 2.1.1", "scopeguard", "serde_json5", + "tikv-jemallocator", "tokio", "tokio-rustls 0.25.0", "tonic 0.11.0", @@ -2864,6 +2865,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.34" diff --git a/Cargo.toml b/Cargo.toml index 004198e7f..119eb15b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,3 +52,6 @@ tonic = { version = "0.11.0", features = ["gzip", "tls"] } tower = "0.4.13" tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } + +[target.'cfg(not(target_env = "msvc"))'.dependencies] +tikv-jemallocator = "0.5.4" diff --git a/src/bin/nativelink.rs b/src/bin/nativelink.rs index ea8141fa2..0cef979c7 100644 --- a/src/bin/nativelink.rs +++ b/src/bin/nativelink.rs @@ -66,6 +66,13 @@ use tower::util::ServiceExt; use tracing::{error, warn}; use tracing_subscriber::filter::{EnvFilter, LevelFilter}; +// We use jemalloc because our program is frequently long-lived and memory fragmentation +// happens often with memory store. Jemalloc nearly completely removes memory fragmentation +// issues. +#[cfg(not(target_env = "msvc"))] +#[global_allocator] +static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + /// Note: This must be kept in sync with the documentation in `PrometheusConfig::path`. const DEFAULT_PROMETHEUS_METRICS_PATH: &str = "/metrics";