build-logic: opt-in parallel cargoBuild for many-core release builds (#perf)
- Commit
bdc7239bfe2e5c84124c2a3b5967981603d9a391- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-10 23:48 UTC (25 days ago)
- Parents
f3038ff
RustToolchainLock caps cargoBuild at one concurrent task, so the ~30
Rust crate/ABI builds of a full release run back to back. Each is fat
LTO with codegen-units = 1, i.e. its final step is single-threaded, so
on a 192-vCPU box the chain took 12 minutes (23 s .. 742 s of a
v2.6.11b assembleRelease, per a per-task Gradle timeline) while every
app with a native lib waited for its turn.
-Pmodernapps.cargoParallelism=N raises the service cap to N (default 1:
unchanged for dev builds, ./install and the small maproj builder). With
N > 1 each crate+ABI also gets its own CARGO_TARGET_DIR under
target/isolated/, because cargo locks its whole build dir and would
otherwise still serialize the builds. Nothing is lost by not sharing
target/: every crate passes its own --remap-path-prefix RUSTFLAGS, so
cargo already rebuilds the shared dependencies per crate.
Safety and bytes:
- The lock exists to stop concurrent implicit `rustup toolchain
install`s; only opt in where the pinned toolchain is preinstalled
(the release builder image installs it in the Dockerfile and step 9).
- The target dir path is not baked into the .so: building e2ee_pqc and
modelrunner into the shared target/ and into an isolated target dir
gave byte-identical libraries.
- Many concurrent cargo processes exceed podman's default 2048-pid
limit ("posix_spawn: Resource temporarily unavailable"); run the
builder with --pids-limit=-1 (Docker's default is unlimited).
Effect (10-app Rust-heavy subset of v2.6.11b, cold compile, 192 vCPUs,
with android.r8.maxWorkers raised as well): 451 s -> 178 s; the last
cargo build now ends at 116 s instead of the chain gating the apps.Diff
build-logic/src/main/kotlin/RustNative.kt
diff --git a/build-logic/src/main/kotlin/RustNative.kt b/build-logic/src/main/kotlin/RustNative.ktindex c60db1b8c2..43927dc73c 100644--- a/build-logic/src/main/kotlin/RustNative.kt+++ b/build-logic/src/main/kotlin/RustNative.kt@@ -31,6 +31,10 @@ import javax.inject.Inject * implicitly by cargo when rust-toolchain.toml pins 1.97.0, which is not * concurrency-safe and produces "Directory not empty (os error 39)", * "cargo not applicable", "failed to install component bin/rust-gdb" errors.+ *+ * `-Pmodernapps.cargoParallelism=N` raises the cap for builds whose toolchain is+ * already installed (the release builder image pre-installs it, so no implicit+ * rustup install can race). See [cargoParallelism]. */ abstract class RustToolchainLock : BuildService<BuildServiceParameters.None> @@ -59,6 +63,22 @@ private fun isRustLengthExempt(path: String): Boolean = path.contains("/examples/") || path.contains("/generated/") || path.contains("/third_party/") +/**+ * How many cargoBuild tasks may run at once: `-Pmodernapps.cargoParallelism=N`+ * (default 1, the historic fully serialized behaviour). The release build on a+ * many-core box sets it to the number of Rust crate/ABI builds so the ~30 fat-LTO+ * (codegen-units = 1, single-threaded) link steps overlap instead of queueing+ * behind one another on the critical path. Only safe when the pinned toolchain is+ * already installed (see [RustToolchainLock]). Read as a plain Int at configuration+ * time, so it is configuration-cache safe.+ */+fun Project.cargoParallelism(): Int =+ providers.gradleProperty("modernapps.cargoParallelism").orNull+ ?.let { raw ->+ raw.toIntOrNull()?.takeIf { it >= 1 }+ ?: throw GradleException("Invalid -Pmodernapps.cargoParallelism=$raw: expected an integer >= 1.")+ } ?: 1+ /** * An Android ABI paired with the Rust target triple and the NDK clang-wrapper prefix * used to cross-compile for it.@@ -361,12 +381,14 @@ fun Project.rustNativeLib( glslcArgs: List<String> = emptyList(), hostTests: Boolean = true, ) {- // Serialize cargoBuild across all Rust modules to avoid concurrent rustup installs.+ // Serialize cargoBuild across all Rust modules to avoid concurrent rustup installs+ // (unless the build opts into parallel cargo, see cargoParallelism below).+ val parallelCargo = cargoParallelism() val rustLock = gradle.sharedServices.registerIfAbsent( "rustToolchainLock", RustToolchainLock::class.java ) {- maxParallelUsages.set(1)+ maxParallelUsages.set(parallelCargo) } val androidApiLevel = 31@@ -472,6 +494,19 @@ fun Project.rustNativeLib( // (current scheme since Cargo.toml workspace unified to root). val perCrateSoOut = file("$srcDir/target/$triple/$profile/lib$crate.so") val workspaceSoOut = rootProject.file("target/$triple/$profile/lib$crate.so")+ // Parallel cargo: cargo locks its whole build dir, so concurrent builds+ // sharing the workspace target/ would still run one at a time. Give each+ // crate+ABI its own target dir instead. No sharing is lost: every crate+ // passes its own --remap-path-prefix RUSTFLAGS, so cargo rebuilds the+ // shared deps per crate in the workspace target/ anyway. The target dir+ // path is not baked into the .so (verified byte-identical vs the shared+ // target/ on the v2.6.11b release build).+ val isolatedTargetDir: File? = if (parallelCargo > 1) {+ rootProject.file("target/isolated/$crate-$abiDir")+ } else {+ null+ }+ val isolatedSoOut = isolatedTargetDir?.let { File(it, "$triple/$profile/lib$crate.so") } val destSo = layout.buildDirectory.file("rustJniLibs/$abiDir/lib$crate.so").get().asFile val targetRustflags = "-C link-arg=-Wl,-z,max-page-size=16384" @@ -596,6 +631,7 @@ fun Project.rustNativeLib( environment("SOURCE_DATE_EPOCH", it) } environment("CARGO_INCREMENTAL", "0")+ isolatedTargetDir?.let { environment("CARGO_TARGET_DIR", it.absolutePath) } commandLine( "$cargoBin/cargo$exeExt",@@ -611,6 +647,8 @@ fun Project.rustNativeLib( doLast { destSo.parentFile.mkdirs() val src = when {+ isolatedSoOut != null -> isolatedSoOut.takeIf { it.exists() }+ ?: error("cargo produced no ${isolatedSoOut.path} for $crate") perCrateSoOut.exists() -> perCrateSoOut workspaceSoOut.exists() -> workspaceSoOut else -> error("Neither per-crate ${perCrateSoOut.path} nor workspace ${workspaceSoOut.path} exists for $crate")