Lesson 1 of 9 · 23m
Instructions, Dependencies, and Why Latency Rules Everything
Model a CPU as a dependency machine rather than a byte-code reader, and learn why chains of dependent work matter more than instruction counts.
A new hire once optimized a parser by deleting arithmetic instructions and made it slower. The real cost was a pointer chase: every byte depended on a load whose address depended on the previous load. Modern CPUs are not tiny machines executing one instruction at a time. They are latency-hiding engines trying to keep dozens or hundreds of independent operations in flight.
The useful mental model
Source code becomes instructions; instructions become micro-operations; the core schedules those operations when their inputs are ready. It can execute independent work in parallel, but a dependency chain forces ordering:
// independent operations: often overlap well
a = x * 3;
b = y + 7;
c = z ^ 0x5a5a;
// dependent chain: each step waits for the previous result
v = table[v];
v = table[v];
v = table[v];The first sequence contains as much work as the second, yet usually finishes much sooner because the operations do not wait on one another.
Throughput is not latency
An integer add might have latency near one cycle and throughput of several per cycle. A division may take dozens of cycles and occupy specialized hardware. A load hitting L1 can cost a few cycles; a miss to DRAM can cost hundreds. Instruction counts hide these differences. Measure the dependency chain, not just the number of operations.
A rough latency ladder worth internalizing:
register dependency ~1 cycle
L1 cache hit ~4 cycles
L2 cache hit ~12-15 cycles
L3 cache hit ~40-75 cycles
main memory ~150-300+ cyclesExact numbers vary by microarchitecture, but the ratios are durable.
Compilers exploit independence aggressively
Compilers unroll loops, hoist invariant calculations, and interleave independent iterations. You can help by avoiding accidental serialization:
// serialized accumulator
for (size_t i = 0; i < n; i++)
sum += values[i];
// multiple accumulators give the core independent chains
uint64_t s0 = 0, s1 = 0, s2 = 0, s3 = 0;
for (size_t i = 0; i + 3 < n; i += 4) {
s0 += values[i];
s1 += values[i + 1];
s2 += values[i + 2];
s3 += values[i + 3];
}
sum = s0 + s1 + s2 + s3;Floating-point addition is not associative, so compilers may not perform this transform automatically under strict semantics.
Amdahl's Law is a dependency warning
If 90 percent of runtime parallelizes and 10 percent is a serial chain, infinite cores still leave that serial 10 percent. Hardware-aware design starts by finding long chains and removing them through better algorithms, batching, precomputation, or changed data layout.
Field notes
- Do not count instructions in assembly and declare victory; count dependency chains and memory operations.
- Division, unpredictable branches, and pointer chasing are latency multipliers in hot loops.
- Compiler output changes with flags and CPU targets; inspect
-O2or-O3output rather than imagining it. - Multiple accumulators help integer reductions, but benchmark before and after because compilers sometimes already do the transform.
Was this lesson helpful?