Photo by Ryan on Unsplash

Speculative Execution: Why Modern CPUs Guess Before They Know


Modern processors don’t wait to know what comes next. They guess, execute the guess, and roll back if they’re wrong. This technique, called speculative execution, is why a 4GHz CPU from 2024 feels orders of magnitude faster than a 4GHz chip from 2004, despite the clock speed staying flat. It’s also why an entire class of security vulnerabilities exists that wouldn’t be possible on simpler hardware.

How Speculation Works

When a CPU encounters a conditional branch—an if statement, a loop exit, anything that could send execution down two different paths—it faces a problem. Resolving the condition takes time. Loading values from memory, performing comparisons, and waiting for earlier instructions to finish can stall the pipeline for dozens of cycles. Instead of waiting, the processor predicts which branch will be taken and starts executing that path immediately.

Branch predictors use pattern history, tracking the last several outcomes of each branch to infer what will happen next. A loop that iterates 1,000 times will be predicted to continue on iteration 437. A branch that alternates true-false-true-false will be predicted to alternate. Modern predictors achieve 95–99% accuracy on real workloads, and when they’re right, the program runs as if the branch never existed.

When they’re wrong, the CPU discards all the speculative work—register updates, memory writes, everything—and restarts from the correct path. The architectural state remains clean. But microarchitectural side effects persist: cache lines loaded during speculation stay loaded, branch predictor entries stay trained, and execution resources stay warmed. This gap between what the architecture promises and what the microarchitecture remembers is where speculation becomes a security problem.

Why It Matters for Performance

Without speculation, every branch is a stall. A processor might execute hundreds of instructions per microsecond, but a single unpredictable branch could halt progress for 10–20 cycles. Speculation converts these stalls into background work: while the branch resolves, dozens of independent instructions continue advancing. On code with heavy branching—parsers, interpreters, database query engines—speculation can double throughput.

Speculation doesn’t just apply to branches. Modern CPUs also speculate on memory dependencies, assuming that a load won’t conflict with an earlier store, and on indirect jumps, predicting function pointer targets before the pointer is fully resolved. Each layer of speculation extracts more instruction-level parallelism from serial code, letting a single core behave like several narrower ones running slightly different futures in parallel.

The Security Trade-off

Spectre and Meltdown, disclosed in 2018, revealed that speculative execution leaks information across security boundaries. An attacker can train the branch predictor to mispredict in a specific way, causing the CPU to speculatively access memory the attacker shouldn’t see. Even though the access is rolled back, it leaves a trace in the cache. By measuring cache timing, the attacker recovers the data.

The fix isn’t to turn off speculation—that would cut performance in half on some workloads. Instead, the industry layered mitigations: serializing instructions that block speculation at privilege boundaries, flushing branch predictor state on context switches, and isolating sensitive data in uncacheable regions. Compilers insert barriers, operating systems adopt new syscalls, and browsers redesign their process models. Each mitigation claws back some of what speculation gave, trading a few percentage points of throughput for isolation guarantees that older architectures provided for free.

What This Means for System Design

Speculative execution assumptions now shape how we build systems. Cryptographic code is written in constant-time patterns to avoid leaking secrets through speculation. Sandboxed languages add explicit speculation barriers around untrusted code. Cloud providers tune hypervisors to minimize cross-tenant speculation channels, and processors ship with new instructions—like Intel’s IBRS and AMD’s retpolines—to fence speculation at architectural boundaries.

Speculation is not going away. It’s too embedded in how modern cores achieve their performance. But the industry has learned that microarchitectural optimizations aren’t invisible. What happens speculatively, even when rolled back, leaves measurable effects. Understanding speculation means understanding why a CPU can’t be modeled as a clean state machine and why performance and security are no longer orthogonal concerns.