Model: Parallel Builds & Amdahl's Law
Ninja runs as many jobs as you have cores by default, and distributed build tools promise hundreds of cores. How much faster does that make a build? Amdahl's law gives the answer for any job with a serial part:
speedup(P) = 1 / (s + (1 - s) / P)s is the fraction of the work that can't run in parallel and P the number of workers. As P grows, the speed-up approaches 1 / s. A build that is 5% serial can never be more than 20× faster, no matter how many machines you add.
What is serial in a build?
| Serial part | Why |
|---|---|
| Configure (CMake, Meson) | runs once before anything compiles |
| Code generators everything depends on | protobuf, Qt moc on a central header, shader compilers |
| The final link | one process; LTO makes it bigger |
| Library chains | app waits for libB waits for libA |
| Packaging, signing, upload | usually one step at the end |
In real builds the "serial fraction" isn't fixed: it's the critical path from the previous lesson. Amdahl's law is the quick back-of-the-envelope version.
The curve
const amdahl = (s, P) => 1 / (s + (1 - s) / P);
const fractions = [0.01, 0.05, 0.10, 0.25];
console.log("cores " + fractions.map((s) => `s=${(s * 100).toFixed(0)}%`.padStart(8)).join(""));
for (const P of [1, 2, 4, 8, 16, 32, 64, 256]) {
const row = fractions.map((s) => (amdahl(s, P).toFixed(1) + "x").padStart(8)).join("");
console.log(String(P).padStart(5) + " " + row);
}
console.log("limit " + fractions.map((s) => ((1 / s).toFixed(0) + "x").padStart(8)).join(""));cores s=1% s=5% s=10% s=25%
1 1.0x 1.0x 1.0x 1.0x
2 2.0x 1.9x 1.8x 1.6x
4 3.9x 3.5x 3.1x 2.3x
8 7.5x 5.9x 4.7x 2.9x
16 13.9x 9.1x 6.4x 3.4x
32 24.4x 12.5x 7.8x 3.7x
64 39.3x 15.4x 8.8x 3.8x
256 72.1x 18.6x 9.7x 4.0x
limit 100x 20x 10x 4x
Read it as a buying guide. With a 10% serial part, going from 8 to 64 cores (8× the hardware) only takes you from 4.7× to 8.8×. With 25% serial, a distributed build farm is almost pointless; you have to shrink the serial part first.
A build-shaped version
Real builds have a fixed serial time plus a pool of equal compile jobs that don't divide perfectly over the cores. This model uses whole jobs ("waves"): 120 compiles of 2 s each, 8 s of configure and a 6 s link.
function buildTime({ configure, link, jobs, jobSeconds }, cores) {
const waves = Math.ceil(jobs / cores); // a wave = one round of parallel compiles
return configure + waves * jobSeconds + link;
}
const b = { configure: 8, link: 6, jobs: 120, jobSeconds: 2 };
const t1 = buildTime(b, 1);
for (const cores of [1, 4, 8, 16, 24, 32, 64, 120, 500]) {
const t = buildTime(b, cores);
console.log(`${String(cores).padStart(3)} cores: ${String(t).padStart(3)} s speedup ${(t1 / t).toFixed(1)}x`);
} 1 cores: 254 s speedup 1.0x
4 cores: 74 s speedup 3.4x
8 cores: 44 s speedup 5.8x
16 cores: 30 s speedup 8.5x
24 cores: 24 s speedup 10.6x
32 cores: 22 s speedup 11.5x
64 cores: 18 s speedup 14.1x
120 cores: 16 s speedup 15.9x
500 cores: 16 s speedup 15.9x
Two things show up that the smooth formula hides:
- Granularity. With 120 jobs there's no difference between 120 and 500 cores: each job is the smallest unit of work. A unity build that merges 120 files into 15 makes this worse for big farms (fewer, longer jobs) even though it reduces total work. That's one of the unity build trade-offs.
- Steps. 24 → 32 cores saves only one wave. Speed-ups come in jumps as the number of waves drops.
Local parallelism limits
Before reaching for a farm, check that local parallelism isn't capped by something else:
- Memory. A heavy template TU can need 2–4 GB. 32 parallel jobs on a 32 GB machine will swap. Ninja job pools (
set_property(GLOBAL PROPERTY JOB_POOLS link=2 heavy=4)+JOB_POOL_COMPILE/JOB_POOL_LINKin CMake) cap memory-hungry steps without lowering-jfor everything. - Links. Several big links at once thrash memory and disk; a
linkpool of 1–2 is common. - I/O. Network file systems and antivirus scanners make many small file accesses slow; parallel jobs queue on them.
makewithout-j. Make runs one job by default. Ninja defaults to cores + 2.
Test yourself
A build is 5% serial. What is the maximum speed-up with unlimited cores?
- 5x
- 20x
- 95x
- Unlimited
In the wave model, why do 120 and 500 cores give the same time?
- Ninja caps jobs at 120
- There are only 120 jobs, so extra cores sit idle
- The link is parallel
- Configure gets slower
What is a CMake/Ninja job pool for?
- Sharing ccache between machines
- Limiting how many jobs of a kind (e.g. links) run at once
- Running tests
- Downloading packages