build(native): enable per-TU parallel compilation (cmake --parallel + /MP)

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 <Runtime.runtime.availableProcessors()>`
  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 <noreply@anthropic.com>
This commit is contained in:
Shirona1337
2026-05-25 01:06:02 +08:00
parent 6529c3090b
commit ecc29d4e82
2 changed files with 13 additions and 2 deletions
+7 -1
View File
@@ -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()
}
}
+6 -1
View File
@@ -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)