Skip to content

Commit 612ea4f

Browse files
authored
perf: add custom memory allocator for improved performance (#2021)
1 parent d26f8a1 commit 612ea4f

File tree

6 files changed

+108
-2
lines changed

6 files changed

+108
-2
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ jobs:
113113
CARGO_PROFILE_OPT_LEVEL: ${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')) && 's' || '0' }}
114114

115115
# Sets the default features which are always used also when `--no-default` is used.
116-
REQUIRED_FEATURES: recipe-generation
116+
# Enable the `performance` feature for release builds to use optimized memory allocators.
117+
REQUIRED_FEATURES: ${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')) && 'recipe-generation,performance' || 'recipe-generation' }}
117118

118119
steps:
119120
- name: Checkout source code

Cargo.lock

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ default-run = "rattler-build"
1212
rust-version = "1.89.0"
1313

1414
[workspace]
15-
members = ["rust-tests", "crates/rattler_build_recipe_generator"]
15+
members = ["rust-tests", "crates/rattler_build_recipe_generator", "crates/rattler_build_allocator"]
1616

1717
[workspace.package]
1818
authors = ["rattler-build contributors <[email protected]>"]
@@ -52,6 +52,10 @@ zip = { version = "6.0.0", default-features = false, features = ["deflate"] }
5252

5353
[features]
5454
default = ['rustls-tls', 'recipe-generation', 's3']
55+
# Enable optimized memory allocators for release builds.
56+
# This provides significant performance improvements, especially on Linux musl.
57+
# Disabled by default due to increased compile time.
58+
performance = ["rattler_build_allocator"]
5559
native-tls = [
5660
'reqwest/native-tls',
5761
'rattler/native-tls',
@@ -219,6 +223,7 @@ dialoguer = "0.12.0"
219223
rattler_build_recipe_generator = { path = "crates/rattler_build_recipe_generator", optional = true, features = [
220224
"cli",
221225
] }
226+
rattler_build_allocator = { path = "crates/rattler_build_allocator", optional = true }
222227

223228
[target.'cfg(not(target_os = "windows"))'.dependencies]
224229
sha2 = { workspace = true, features = ["asm"] }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "rattler_build_allocator"
3+
version = "0.1.0"
4+
authors.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
repository.workspace = true
8+
description = "A crate that provides the best memory allocator for different platforms"
9+
10+
[lib]
11+
doctest = false
12+
13+
# Use mimalloc on Windows
14+
[target.'cfg(windows)'.dependencies]
15+
mimalloc = "0.1.43"
16+
17+
# Use jemalloc on supported unix platforms (excluding OpenBSD and FreeBSD)
18+
[target.'cfg(all(not(windows), not(target_os = "openbsd"), not(target_os = "freebsd"), any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "powerpc64")))'.dependencies]
19+
tikv-jemallocator = "0.6.0"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//! This crate provides the best memory allocator for different platforms.
2+
//!
3+
//! On Windows, we use mimalloc because it provides good performance.
4+
//! On most Unix platforms (Linux, macOS), we use jemalloc for its excellent
5+
//! performance characteristics with multi-threaded applications.
6+
//!
7+
//! This crate is designed to be used as a dependency that, when included,
8+
//! automatically sets the global allocator. Simply add this crate as a
9+
//! dependency and the allocator will be configured.
10+
11+
// Use mimalloc on Windows
12+
#[cfg(windows)]
13+
#[global_allocator]
14+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
15+
16+
// Use jemalloc on supported unix platforms
17+
#[cfg(all(
18+
not(windows),
19+
not(target_os = "openbsd"),
20+
not(target_os = "freebsd"),
21+
any(
22+
target_arch = "x86_64",
23+
target_arch = "aarch64",
24+
target_arch = "powerpc64"
25+
)
26+
))]
27+
#[global_allocator]
28+
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
//! This is the main entry point for the `rattler-build` binary.
22
3+
// Use custom allocators for improved performance when the `performance` feature is enabled.
4+
// This must be at the crate root to set the global allocator.
5+
#[cfg(feature = "performance")]
6+
use rattler_build_allocator as _;
7+
38
use std::{
49
fs::File,
510
io::{self, IsTerminal},

0 commit comments

Comments
 (0)