From 5151db602fb4495daa09d0e42668a0b724509ab8 Mon Sep 17 00:00:00 2001 From: "Brian G. Milnes" Date: Wed, 18 Mar 2026 18:17:38 -0700 Subject: [PATCH] Use jemalloc as global allocator on unix On Linux, switching from glibc malloc to jemalloc saves avg 12%, peak 14% RSS and avg 9%, peak 14% wall time across 6 open-source Verus projects, with zero code changes. macOS is not yet benchmarked. jemalloc is not available on Windows. The `unprefixed_malloc_on_supported_platforms` feature is required so that jemalloc replaces C-level malloc/free globally, not just Rust's allocator. Without it, librustc_driver.so (which uses C malloc via LLVM) fights with jemalloc over the heap, causing corruption during vstd build. Benchmarks and analysis: https://github.com/briangmilnes/verus-heaptrack Co-Authored-By: Claude Opus 4.6 --- source/Cargo.lock | 21 +++++++++++++++++++++ source/rust_verify/Cargo.toml | 3 +++ source/rust_verify/src/main.rs | 4 ++++ 3 files changed, 28 insertions(+) diff --git a/source/Cargo.lock b/source/Cargo.lock index ad08880d74..ffdec9d551 100644 --- a/source/Cargo.lock +++ b/source/Cargo.lock @@ -1902,6 +1902,7 @@ dependencies = [ "serde_json", "sha2", "sise", + "tikv-jemallocator", "vir", "win32job", ] @@ -2386,6 +2387,26 @@ dependencies = [ "syn 2.0.101", ] +[[package]] +name = "tikv-jemalloc-sys" +version = "0.6.1+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd8aa5b2ab86a2cefa406d889139c162cbb230092f7d1d7cbc1716405d852a3b" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "tikv-jemallocator" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0359b4327f954e0567e69fb191cf1436617748813819c94b8cd4a431422d053a" +dependencies = [ + "libc", + "tikv-jemalloc-sys", +] + [[package]] name = "time" version = "0.3.41" diff --git a/source/rust_verify/Cargo.toml b/source/rust_verify/Cargo.toml index eabc04a665..edfa98f7d6 100644 --- a/source/rust_verify/Cargo.toml +++ b/source/rust_verify/Cargo.toml @@ -24,6 +24,9 @@ console = { version = "0.15", default-features = false, features = ["ansi-parsin indexmap = { version = "1" } rustc_mir_build_verus = { path = "../rustc_mir_build", package = "rustc_mir_build" } +[target.'cfg(unix)'.dependencies] +tikv-jemallocator = { version = "0.6", features = ["unprefixed_malloc_on_supported_platforms"] } + [target.'cfg(windows)'.dependencies] win32job = "1" diff --git a/source/rust_verify/src/main.rs b/source/rust_verify/src/main.rs index 7210bf4991..db25c0e70f 100644 --- a/source/rust_verify/src/main.rs +++ b/source/rust_verify/src/main.rs @@ -1,5 +1,9 @@ #![feature(rustc_private)] +#[cfg(target_family = "unix")] +#[global_allocator] +static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + use rust_verify::util::{VerusBuildProfile, verus_build_info}; extern crate rustc_driver;