From 476e09ad569ff03ec8a6690ae6b8aeb31891f209 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 27 Oct 2026 10:45:06 -0400 Subject: [PATCH] Make mimalloc a default Cargo feature Allow builders to select the system allocator while keeping mimalloc in ordinary Cargo and Bazel builds. Derive the Bazel binary features from the root Cargo package so its defaults stay in sync. Codex for Tamir. --- Cargo.toml | 4 ++-- crate_deps.bzl | 4 ++++ src/BUILD | 11 ++++++++++- src/main.rs | 1 + 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1601e47f..e75d30f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ name = "bazel-diff" path = "src/main.rs" [features] -default = ["vendored-protoc"] +default = ["mimalloc", "vendored-protoc"] vendored-protoc = ["dep:protoc-bin-vendored"] [dependencies] @@ -24,7 +24,7 @@ buffa = "0.9.1" clap = { version = "4.5", features = ["derive", "env"] } flate2 = "1.1" hex = "0.4" -mimalloc = "0.1" +mimalloc = { version = "0.1", optional = true } rayon = "1.10" rustls-pki-types = "1.15" rust-s3 = { version = "=0.36.0", default-features = false, features = ["sync-rustls-tls"] } diff --git a/crate_deps.bzl b/crate_deps.bzl index e2e718d3..a42e1089 100644 --- a/crate_deps.bzl +++ b/crate_deps.bzl @@ -32,6 +32,10 @@ def _deps(kinds): deps.extend(data[kind]) return sorted(deps) +def crate_features(exclude = []): + """Enabled Cargo features of the root package.""" + return [feature for feature in _root_package()["crate_features"] if feature not in exclude] + def normal_crate_deps(): """Normal dependencies of the root Cargo package.""" return _deps(["deps"]) diff --git a/src/BUILD b/src/BUILD index a1d4e5f8..73e9e9b3 100644 --- a/src/BUILD +++ b/src/BUILD @@ -2,7 +2,7 @@ load("@rules_rs//rs:cargo_build_script.bzl", "cargo_build_script") load("@rules_rs//rs:rust_binary.bzl", "rust_binary") load("@rules_rs//rs:rust_library.bzl", "rust_library") load("@rules_rs//rs:rust_test.bzl", "rust_test") -load("//:crate_deps.bzl", "normal_crate_deps") +load("//:crate_deps.bzl", "crate_features", "normal_crate_deps") load("//tools/coverage:defs.bzl", "coverage_enforced_test") cargo_build_script( @@ -39,9 +39,18 @@ rust_library( deps = normal_crate_deps() + [":build_script"], ) +config_setting( + name = "without_mimalloc", + values = {"features": "-mimalloc"}, +) + rust_binary( name = "bazel-diff", srcs = ["main.rs"], + crate_features = select({ + ":without_mimalloc": crate_features(exclude = ["mimalloc"]), + "//conditions:default": crate_features(), + }), crate_root = "main.rs", rustc_env = { "CARGO_PKG_VERSION": module_version() if module_version() else "unknown", diff --git a/src/main.rs b/src/main.rs index 1602a5cc..5e4dbe6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use std::path::{Path, PathBuf}; use std::time::Duration; // Use mimalloc's per-thread heaps for parallel protobuf decoding in generate-hashes. +#[cfg(feature = "mimalloc")] #[global_allocator] static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;