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