Skip to content

Commit 03bf37f

Browse files
committed
alloc: use jemalloc when building with musl
It turns out that musl's allocator is slow enough to cause a fairly noticeable performance regression when ripgrep is built as a static binary with musl. We fix this by using jemalloc when building with musl. We continue to use the default system allocator in all other scenarios. Namely, glibc's allocator doesn't noticeably regress performance compared to jemalloc. But we could add more targets to this logic if other system allocators (macOS, Windows) prove to be slow. This wasn't necessary before because rustc recently stopped using jemalloc by default. Fixes #1268
1 parent e7829c0 commit 03bf37f

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

Cargo.lock

Lines changed: 28 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ version = "2.32.0"
6161
default-features = false
6262
features = ["suggestions"]
6363

64+
[target.'cfg(target_env = "musl")'.dependencies.jemallocator]
65+
version = "0.3.0"
66+
6467
[build-dependencies]
6568
lazy_static = "1.1.0"
6669

src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ mod path_printer;
1919
mod search;
2020
mod subject;
2121

22+
// Since Rust no longer uses jemalloc by default, ripgrep will, by default,
23+
// use the system allocator. On Linux, this would normally be glibc's
24+
// allocator, which is pretty good. In particular, ripgrep does not have a
25+
// particularly allocation heavy workload, so there really isn't much
26+
// difference (for ripgrep's purposes) between glibc's allocator and jemalloc.
27+
//
28+
// However, when ripgrep is built with musl, this means ripgrep will use musl's
29+
// allocator, which appears to be substantially worse. (musl's goal is not to
30+
// have the fastest version of everything. Its goal is to be small and amenable
31+
// to static compilation.) Even though ripgrep isn't particularly allocation
32+
// heavy, musl's allocator appears to slow down ripgrep quite a bit. Therefore,
33+
// when building with musl, we use jemalloc.
34+
//
35+
// We don't unconditionally use jemalloc because it can be nice to use the
36+
// system's default allocator by default. Moreover, jemalloc seems to increase
37+
// compilation times by a bit.
38+
#[cfg(target_env = "musl")]
39+
#[global_allocator]
40+
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
41+
2242
type Result<T> = ::std::result::Result<T, Box<::std::error::Error>>;
2343

2444
fn main() {

0 commit comments

Comments
 (0)