Respect jobserver set by Cargo
The jemalloc-sys crate currently does not respect the Make jobserver protocol, and hardcodes -j$NUM_JOBS Make jobs as set by Cargo.
Cargo recommends to use CARGO_MAKEFLAGS instead (https://doc.rust-lang.org/cargo/reference/environment-variables.html), to avoid oversubscribing CPU cores when building Jemalloc C++ code in parallel with other Rust crates.
Here is a small benchmark that I did on hyperqueue with and without jobserver protocol support on AMD Zen 3 with 8 cores enabled:
/pr/it/hyperqueue [main/u]$ hyperfine --warmup 1 --runs 2 --prepare "rm -rf target" "cargo build --release" "JEMALLOC_JOBSERVER=1 cargo build --release" "cargo build" "JEMALLOC_JOBSERVER=1 cargo build"
Benchmark 1: cargo build --release
Time (mean ± σ): 57.620 s ± 0.010 s [User: 334.019 s, System: 37.222 s]
Range (min … max): 57.613 s … 57.626 s 2 runs
Benchmark 2: JEMALLOC_JOBSERVER=1 cargo build --release
Time (mean ± σ): 56.969 s ± 0.017 s [User: 324.140 s, System: 36.739 s]
Range (min … max): 56.956 s … 56.981 s 2 runs
Benchmark 3: cargo build
Time (mean ± σ): 45.410 s ± 0.056 s [User: 193.525 s, System: 37.347 s]
Range (min … max): 45.371 s … 45.450 s 2 runs
Benchmark 4: JEMALLOC_JOBSERVER=1 cargo build
Time (mean ± σ): 44.039 s ± 0.011 s [User: 194.451 s, System: 37.357 s]
Range (min … max): 44.031 s … 44.046 s 2 runs
With jobserver protocol support, the compilation finishes 2-3% faster.
Added the signoff.
MAKEFLAGS have not been sanitized, so the jobs fail.
-j and -jN and -j N have to be removed when --jobserver-fds is present.
MAKEFLAGS="-j --jobserver-fds=8,9"
https://github.com/tikv/jemallocator/issues/92#issuecomment-3536269176
│ │ running: cd "$SRC_DIR/src/rust/target/x86_64-unknown-linux-gnu/release/build/tikv-jemalloc-sys-cde55cd13423d9e3/out/build" && MAKEFLAGS=" -- OBJECTS=init.o SHLIB=polars.so -j --jobserver-fds=8,9 --jobserver-auth=8,9" "make" │ │ make[1]: Entering directory '$SRC_DIR/src/rust/target/x86_64-unknown-linux-gnu/release/build/tikv-jemalloc-sys-cde55cd13423d9e3/out/build' │ │ make[1]: Leaving directory '$SRC_DIR/src/rust/target/x86_64-unknown-linux-gnu/release/build/tikv-jemalloc-sys-cde55cd13423d9e3/out/build' │ │ --- stderr │ │ make[1]: *** No rule to make target '-j'. Stop. │ │ thread 'main' (6599) panicked at $BUILD_PREFIX/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tikv-jemalloc-sys-0.6.1+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7/build.rs:411:9: │ │ command did not execute successfully: cd "$SRC_DIR/src/rust/target/x86_64-unknown-linux-gnu/release/build/tikv-jemalloc-sys-cde55cd13423d9e3/out/build" && MAKEFLAGS=" -- OBJECTS=init.o SHLIB=polars.so -j --jobserver-fds=8,9 --jobserver-auth=8,9" "make"