From 03bf37ff4a29361c47843369f7d3dc5689b8fdac Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Wed, 24 Apr 2019 17:21:38 -0400 Subject: [PATCH] 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 --- Cargo.lock | 28 ++++++++++++++++++++++++++++ Cargo.toml | 3 +++ src/main.rs | 20 ++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 9f7343a7..5935b6cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,6 +125,11 @@ name = "fnv" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "fs_extra" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "fuchsia-cprng" version = "0.1.1" @@ -257,6 +262,25 @@ name = "itoa" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "jemalloc-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)", + "fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "jemallocator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "jemalloc-sys 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lazy_static" version = "1.3.0" @@ -508,6 +532,7 @@ dependencies = [ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "grep 0.2.4", "ignore 0.4.7", + "jemallocator 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -707,9 +732,12 @@ dependencies = [ "checksum encoding_rs 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)" = "4155785c79f2f6701f185eb2e6b4caf0555ec03477cb4c70db67b465311620ed" "checksum encoding_rs_io 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9619ee7a2bf4e777e020b95c1439abaf008f8ea8041b78a0552c4f1bcf4df32c" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" +"checksum fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674" "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" +"checksum jemalloc-sys 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7bef0d4ce37578dfd80b466e3d8324bd9de788e249f1accebb0c472ea4b52bdc" +"checksum jemallocator 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c2b69163a3cf2d0fffcd4e1b57921bc6d8fb97ec27f2aeef00562abdaf4ffe2a" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" "checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917" "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" diff --git a/Cargo.toml b/Cargo.toml index e93c41a8..d8c68d75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,6 +61,9 @@ version = "2.32.0" default-features = false features = ["suggestions"] +[target.'cfg(target_env = "musl")'.dependencies.jemallocator] +version = "0.3.0" + [build-dependencies] lazy_static = "1.1.0" diff --git a/src/main.rs b/src/main.rs index f607422d..d5c67a0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,26 @@ mod path_printer; mod search; mod subject; +// Since Rust no longer uses jemalloc by default, ripgrep will, by default, +// use the system allocator. On Linux, this would normally be glibc's +// allocator, which is pretty good. In particular, ripgrep does not have a +// particularly allocation heavy workload, so there really isn't much +// difference (for ripgrep's purposes) between glibc's allocator and jemalloc. +// +// However, when ripgrep is built with musl, this means ripgrep will use musl's +// allocator, which appears to be substantially worse. (musl's goal is not to +// have the fastest version of everything. Its goal is to be small and amenable +// to static compilation.) Even though ripgrep isn't particularly allocation +// heavy, musl's allocator appears to slow down ripgrep quite a bit. Therefore, +// when building with musl, we use jemalloc. +// +// We don't unconditionally use jemalloc because it can be nice to use the +// system's default allocator by default. Moreover, jemalloc seems to increase +// compilation times by a bit. +#[cfg(target_env = "musl")] +#[global_allocator] +static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; + type Result = ::std::result::Result>; fn main() {