Model: The Critical Path of a Build
Give a build more cores and it gets faster, until suddenly it doesn't. The reason is the critical path: the longest chain of steps where each one has to wait for the previous one. No amount of parallelism can make the build shorter than that chain. Two numbers bound every build:
- total work
W: the sum of all step durations (what one core would take); - critical path
C: the longest dependency chain.
With P cores the build can't take less than max(C, W / P). When W / P dominates, more cores help. When C dominates, only shortening the chain helps: split a huge TU, break a dependency, make a code generator faster, or stop serialising on one big library.
Computing it from a log
The model below uses a fixed .ninja_log excerpt (durations from a real build of a small game engine, frozen so the output never changes) and the dependency edges you'd get from ninja -t query. It computes the earliest finish time of every step in topological order, then walks back from the last step to recover the chain.
// .ninja_log v7 lines: start \t end \t mtime \t output \t hash (frozen sample)
const log = `# ninja log v7
0\t2100\t0\tobj/math.o\taa01
0\t3900\t0\tobj/render.o\taa02
0\t1200\t0\tobj/audio.o\taa03
0\t5200\t0\tobj/shaders_gen.h\taa04
5200\t9800\t0\tobj/shaders.o\taa05
0\t2600\t0\tobj/physics.o\taa06
9800\t10300\t0\tlibengine.a\taa07
0\t1500\t0\tobj/game.o\taa08
10300\t12700\t0\tgame\taa09`;
// inputs of each output (from `ninja -t query`)
const deps = {
"obj/shaders.o": ["obj/shaders_gen.h"],
"libengine.a": ["obj/math.o", "obj/render.o", "obj/audio.o", "obj/shaders.o", "obj/physics.o"],
"game": ["obj/game.o", "libengine.a"],
};
const dur = {};
for (const line of log.split("\n").slice(1)) {
const [start, end, , out] = line.split("\t");
dur[out] = Number(end) - Number(start);
}
const finish = {}, via = {};
function earliestFinish(out) {
if (finish[out] !== undefined) return finish[out];
let ready = 0;
for (const d of deps[out] ?? []) {
const f = earliestFinish(d);
if (f > ready) { ready = f; via[out] = d; }
}
return (finish[out] = ready + dur[out]);
}
const W = Object.values(dur).reduce((a, b) => a + b, 0);
const C = earliestFinish("game");
const path = [];
for (let o = "game"; o; o = via[o]) path.unshift(`${o} (${dur[o]} ms)`);
console.log(`total work W = ${W} ms`);
console.log(`critical path C = ${C} ms`);
console.log("chain:", path.join(" -> "));
for (const P of [1, 2, 4, 8, 64]) {
const bound = Math.max(C, Math.ceil(W / P));
console.log(`P=${String(P).padStart(2)} lower bound ${bound} ms (${bound === C ? "path-bound" : "work-bound"})`);
}total work W = 24000 ms
critical path C = 12700 ms
chain: obj/shaders_gen.h (5200 ms) -> obj/shaders.o (4600 ms) -> libengine.a (500 ms) -> game (2400 ms)
P= 1 lower bound 24000 ms (work-bound)
P= 2 lower bound 12700 ms (path-bound)
P= 4 lower bound 12700 ms (path-bound)
P= 8 lower bound 12700 ms (path-bound)
P=64 lower bound 12700 ms (path-bound)
From two cores upwards this build is path-bound: a code generator (shaders_gen.h) and the big file that consumes it form a 9.8-second chain, and everything else finishes long before. Buying a 64-core machine changes nothing.
Shortening the chain
The same model answers "what if?" questions. Suppose you split shaders.o into two TUs that each take half the time and can run in parallel, and make the generator twice as fast:
const dur = { gen: 5200, shaders: 4600, lib: 500, link: 2400, render: 3900, physics: 2600, math: 2100, audio: 1200, game: 1500 };
function critical(d, splitShaders) {
const shaders = splitShaders ? d.shaders / 2 : d.shaders; // two halves run side by side
const engineReady = Math.max(d.gen + shaders, d.render, d.physics, d.math, d.audio) + d.lib;
return Math.max(engineReady, d.game) + d.link;
}
console.log("before: ", critical(dur, false), "ms");
console.log("split shaders.o: ", critical(dur, true), "ms");
console.log("+ generator 2x faster: ", critical({ ...dur, gen: dur.gen / 2 }, true), "ms");before: 12700 ms
split shaders.o: 10400 ms
+ generator 2x faster: 7800 ms
After both changes the chain is gen → shaders half → lib → link at 7.8 s, and render.o (3.9 s) is close to becoming the next bottleneck. That's the usual rhythm of critical-path work: fix the longest chain, recompute, repeat.
Reading a real log
To do this on your own build:
- Build from clean with full parallelism, so start times reflect real scheduling.
- Take the newest entry per output from
.ninja_logfor durations. - Get edges from
ninja -t query <output>(or the-t graphoutput) for the outputs on suspicious chains. - Look for long serial steps: code generators, one huge TU, a big static library everyone waits for, and the final link (see LTO and Faster Links).
A timeline view makes the critical path visible at a glance: convert the log with ninjatracing and open it in ui.perfetto.dev. Idle cores at the end of the build are the critical path showing itself.
Test yourself
W = 24.0 s of total work and C = 12.7 s of critical path. What is the best possible time on 8 cores?
- 3.0 s
- 12.7 s
- 24.4 s
- 1.6 s
A build is path-bound. What helps?
- More cores
- Shortening the longest dependency chain (split a TU, speed up a generator)
- A bigger ccache
- Running Ninja with -j1
Why does the model take dependency edges in addition to the log?
- The log has no durations
- The log lists durations but not which step waits for which
- Edges make it faster
- To count headers