From ecc29d4e823da7f116619259af28324adfc7ce36 Mon Sep 17 00:00:00 2001 From: Shirona1337 Date: Mon, 25 May 2026 01:06:02 +0800 Subject: [PATCH] build(native): enable per-TU parallel compilation (cmake --parallel + /MP) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before: MSBuild defaulted to a single process per project, so each vcxproj compiled its .cpp files sequentially. Local clean rebuild of OpenZen.dll + OpenZenLoader.exe measured ~14.6 s. After: same cold rebuild measures ~4.8 s — 3x faster — by combining - `cmake --build --parallel ` in the buildNative task, which forwards a project-level `-m:N` to MSBuild and gets DLL / Loader to build concurrently where the dependency graph allows. - `/MP` on the MSVC compile options, which is the actual win: it lets cl.exe spawn one compiler process per available core within a single vcxproj. The DLL+Loader together have ~13 .cpp files; this is where the bulk of wall-time was being spent serially. CI runners get the same speed-up for free. Co-Authored-By: Claude Opus 4.7 --- build.gradle | 8 +++++++- native/CMakeLists.txt | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 4f4028c..eada613 100644 --- a/build.gradle +++ b/build.gradle @@ -405,7 +405,13 @@ tasks.register('buildNative', Exec) { doFirst { def cmake = findCmake() if (!cmake) throw new GradleException(cmakeMissingMessage) - commandLine cmake, '--build', 'build', '--config', 'Release' + // --parallel without a number lets CMake pick the host's logical CPU + // count and forwards it to the underlying generator (MSBuild gets /m, + // Ninja gets -j). MSBuild defaults to a single process otherwise. + def jobs = Runtime.runtime.availableProcessors() + logger.lifecycle("Parallel jobs: ${jobs}") + commandLine cmake, '--build', 'build', '--config', 'Release', + '--parallel', jobs.toString() } } diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index d726be3..36aa21e 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -37,7 +37,12 @@ add_compile_definitions( ) if(MSVC) - add_compile_options(/W3 /permissive- /utf-8 /EHsc) + # /MP enables cl.exe-level multi-process compilation: every translation + # unit inside a single vcxproj compiles in parallel. Without it, MSBuild + # only parallelises *across* projects (the -j N forwarded by + # `cmake --build --parallel` controls that), so a single project with + # a dozen .cpp files would still compile serially. + add_compile_options(/W3 /permissive- /utf-8 /EHsc /MP) endif() add_subdirectory(dll)