1From 68c2a4d7a5d9b46f65121958fdb12d5270bfd1b6 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--- 13 .github/workflows/ci.yml | 6 ++++++ 14 .github/workflows/release.yml | 8 +++++++- 15 Cargo.toml | 8 +++++++- 16 README.md | 9 +++++++++ 17 crates/core/main.rs | 8 ++++++-- 18 5 files changed, 35 insertions(+), 4 deletions(-) 19 20diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml 21index ab154ec..aa567d9 100644 22--- a/.github/workflows/ci.yml 23+++ b/.github/workflows/ci.yml 24@@ -149,6 +149,12 @@ jobs: 25 if: matrix.target != '' 26 run: ${{ env.CARGO }} test --verbose --workspace ${{ env.TARGET_FLAGS }} 27 28+ - name: Run tests with jemalloc (Musl) 29+ # We only use the jemalloc allocator when building with musl. 30+ # The system allocator is good enough for other platforms. 31+ if: matrix.os == 'nightly-musl' 32+ run: ${{ env.CARGO }} test --verbose --all --features jemalloc ${{ env.TARGET_FLAGS }} 33+ 34 - name: Test for existence of build artifacts (Windows) 35 if: matrix.os == 'windows-2019' 36 shell: bash 37diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml 38index 7cfb6a4..ad6b82d 100644 39--- a/.github/workflows/release.yml 40+++ b/.github/workflows/release.yml 41@@ -133,7 +133,13 @@ jobs: 42 echo "target flag is: ${{ env.TARGET_FLAGS }}" 43 echo "target dir is: ${{ env.TARGET_DIR }}" 44 45- - name: Build release binary 46+ - name: Build release binary (linux) 47+ if: matrix.build == 'linux' 48+ # Use jemalloc allocator for much better performance over the musl default allocator 49+ run: ${{ env.CARGO }} build --verbose --release --features "pcre2 jemalloc" ${{ env.TARGET_FLAGS }} 50+ 51+ - name: Build release binary (non-linux) 52+ if: matrix.build != 'linux' 53 run: ${{ env.CARGO }} build --verbose --release --features pcre2 ${{ env.TARGET_FLAGS }} 54 55 - name: Strip release binary (linux and macos) 56diff --git a/Cargo.toml b/Cargo.toml 57index fb78fcb..0d34b1e 100644 58--- a/Cargo.toml 59+++ b/Cargo.toml 60@@ -56,8 +56,9 @@ version = "2.33.0" 61 default-features = false 62 features = ["suggestions"] 63 64-[target.'cfg(all(target_env = "musl", target_pointer_width = "64"))'.dependencies.jemallocator] 65+[dependencies.jemallocator] 66 version = "0.3.0" 67+optional = true 68 69 [build-dependencies] 70 lazy_static = "1.1.0" 71@@ -75,6 +76,11 @@ walkdir = "2" 72 [features] 73 simd-accel = ["grep/simd-accel"] 74 pcre2 = ["grep/pcre2"] 75+# The jemalloc allocator is used for improved 76+# performance on x86 musl builds. 77+# Cargo does not yet support platform-specific features 78+# https://github.com/rust-lang/cargo/issues/1197 79+jemalloc = ["jemallocator"] 80 81 [profile.release] 82 debug = 1 83diff --git a/README.md b/README.md 84index 46938bc..9917b29 100644 85--- a/README.md 86+++ b/README.md 87@@ -406,6 +406,15 @@ build a static executable with MUSL and with PCRE2, then you will need to have 88 `musl-gcc` installed, which might be in a separate package from the actual 89 MUSL library, depending on your Linux distribution. 90 91+When building with the MUSL target on Linux, it is recommended to use the 92+jemalloc allocator for performance: 93+ 94+``` 95+$ rustup target add x86_64-unknown-linux-musl 96+$ cargo build --release --target x86_64-unknown-linux-musl --features jemalloc 97+``` 98+ 99+ 100 101 ### Running tests 102 103diff --git a/crates/core/main.rs b/crates/core/main.rs 104index 47385de..c9dae5a 100644 105--- a/crates/core/main.rs 106+++ b/crates/core/main.rs 107@@ -31,7 +31,7 @@ mod subject; 108 // have the fastest version of everything. Its goal is to be small and amenable 109 // to static compilation.) Even though ripgrep isn't particularly allocation 110 // heavy, musl's allocator appears to slow down ripgrep quite a bit. Therefore, 111-// when building with musl, we use jemalloc. 112+// we expose a feature for using jemalloc when building with musl. 113 // 114 // We don't unconditionally use jemalloc because it can be nice to use the 115 // system's default allocator by default. Moreover, jemalloc seems to increase 116@@ -39,7 +39,11 @@ mod subject; 117 // 118 // Moreover, we only do this on 64-bit systems since jemalloc doesn't support 119 // i686. 120-#[cfg(all(target_env = "musl", target_pointer_width = "64"))] 121+#[cfg(all( 122+ target_env = "musl", 123+ target_pointer_width = "64", 124+ feature = "jemalloc" 125+))] 126 #[global_allocator] 127 static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; 128 129-- 1302.32.0 131 132