1From 77f850969433b14769ade4281899373f3ebabf86 Mon Sep 17 00:00:00 2001 2From: Jonathan Stites <mail@jonstites.com> 3Date: Wed, 6 May 2020 12:55:35 +0000 4Subject: [PATCH] puts jemalloc allocator behind a cargo feature flag 5 6Retrieved from: https://github.com/BurntSushi/ripgrep/pull/1569 7 8Moves jemalloc behind a feature for musl builds, where it is not 9supported by the upstream project, so ripgrep will fail to build. 10 11Signed-off-by: Sam Voss <sam.voss@gmail.com> 12[Antoine: update for 14.1.0] 13Signed-off-by: Antoine Coutant <antoine.coutant@smile.fr> 14--- 15 .github/workflows/ci.yml | 6 ++++++ 16 .github/workflows/release.yml | 8 +++++++- 17 Cargo.toml | 8 +++++++- 18 README.md | 9 +++++++++ 19 crates/core/main.rs | 8 ++++++-- 20 5 files changed, 35 insertions(+), 4 deletions(-) 21 22diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml 23index d21b85a..0c9ecb9 100644 24--- a/.github/workflows/ci.yml 25+++ b/.github/workflows/ci.yml 26@@ -172,6 +172,12 @@ jobs: 27 if: matrix.target != '' 28 run: ${{ env.CARGO }} test --verbose --workspace ${{ env.TARGET_FLAGS }} 29 30+ - name: Run tests with jemalloc (Musl) 31+ # We only use the jemalloc allocator when building with musl. 32+ # The system allocator is good enough for other platforms. 33+ if: matrix.os == 'nightly-musl' 34+ run: ${{ env.CARGO }} test --verbose --all --features jemalloc ${{ env.TARGET_FLAGS }} 35+ 36 - name: Test zsh shell completions (Unix, sans cross) 37 # We could test this when using Cross, but we'd have to execute the 38 # 'rg' binary (done in test-complete) with qemu, which is a pain and 39diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml 40index f6ea3d9..ac18129 100644 41--- a/.github/workflows/release.yml 42+++ b/.github/workflows/release.yml 43@@ -171,7 +171,13 @@ jobs: 44 echo "target flag is: ${{ env.TARGET_FLAGS }}" 45 echo "target dir is: ${{ env.TARGET_DIR }}" 46 47- - name: Build release binary 48+ - name: Build release binary (linux) 49+ if: matrix.build == 'linux' 50+ # Use jemalloc allocator for much better performance over the musl default allocator 51+ run: ${{ env.CARGO }} build --verbose --release --features "pcre2 jemalloc" ${{ env.TARGET_FLAGS }} 52+ 53+ - name: Build release binary (non-linux) 54+ if: matrix.build != 'linux' 55 shell: bash 56 run: | 57 ${{ env.CARGO }} build --verbose --release --features pcre2 ${{ env.TARGET_FLAGS }} 58diff --git a/Cargo.toml b/Cargo.toml 59index da350bc..1a0a48f 100644 60--- a/Cargo.toml 61+++ b/Cargo.toml 62@@ -59,8 +59,9 @@ serde_json = "1.0.23" 63 termcolor = "1.1.0" 64 textwrap = { version = "0.16.0", default-features = false } 65 66-[target.'cfg(all(target_env = "musl", target_pointer_width = "64"))'.dependencies.jemallocator] 67+[dependencies.jemallocator] 68 version = "0.5.0" 69+optional = true 70 71 [dev-dependencies] 72 serde = "1.0.77" 73@@ -70,6 +71,11 @@ walkdir = "2" 74 [features] 75 simd-accel = ["grep/simd-accel"] 76 pcre2 = ["grep/pcre2"] 77+# The jemalloc allocator is used for improved 78+# performance on x86 musl builds. 79+# Cargo does not yet support platform-specific features 80+# https://github.com/rust-lang/cargo/issues/1197 81+jemalloc = ["jemallocator"] 82 83 [profile.release] 84 debug = 1 85diff --git a/README.md b/README.md 86index 0821fab..fdb9fb5 100644 87--- a/README.md 88+++ b/README.md 89@@ -478,6 +478,15 @@ build a static executable with MUSL and with PCRE2, then you will need to have 90 `musl-gcc` installed, which might be in a separate package from the actual 91 MUSL library, depending on your Linux distribution. 92 93+When building with the MUSL target on Linux, it is recommended to use the 94+jemalloc allocator for performance: 95+ 96+``` 97+$ rustup target add x86_64-unknown-linux-musl 98+$ cargo build --release --target x86_64-unknown-linux-musl --features jemalloc 99+``` 100+ 101+ 102 103 ### Running tests 104 105diff --git a/crates/core/main.rs b/crates/core/main.rs 106index 64f35ce..9aa6663 100644 107--- a/crates/core/main.rs 108+++ b/crates/core/main.rs 109@@ -27,7 +27,7 @@ mod search; 110 // have the fastest version of everything. Its goal is to be small and amenable 111 // to static compilation.) Even though ripgrep isn't particularly allocation 112 // heavy, musl's allocator appears to slow down ripgrep quite a bit. Therefore, 113-// when building with musl, we use jemalloc. 114+// we expose a feature for using jemalloc when building with musl. 115 // 116 // We don't unconditionally use jemalloc because it can be nice to use the 117 // system's default allocator by default. Moreover, jemalloc seems to increase 118@@ -35,7 +35,11 @@ mod search; 119 // 120 // Moreover, we only do this on 64-bit systems since jemalloc doesn't support 121 // i686. 122-#[cfg(all(target_env = "musl", target_pointer_width = "64"))] 123+#[cfg(all( 124+ target_env = "musl", 125+ target_pointer_width = "64", 126+ feature = "jemalloc" 127+))] 128 #[global_allocator] 129 static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; 130 131-- 1322.25.1 133 134