liveBook · Manning

Speculative execution

As explained in Latency

Speculative execution is a processor technique that executes predicted future instructions before their dependencies or control-flow conditions are confirmed.

Speculative execution performs work before it is certain that the work will be needed. A CPU may execute a predicted path or load data early to hide latency. If the result is needed, it is already available; otherwise, the speculative result is discarded. section 11.4

The same idea applies beyond processors. First identify an operation whose latency is hard to reduce, then predict when its result will be needed and start it early. Prediction is the difficult part: an accurate guess hides latency, while a wrong one spends resources without delivering a useful result. section 11.1 section 11.4

Load data while a user authenticates

A web application can prefetch user-specific data in parallel with authentication. If that data is needed when login finishes, its access latency has been hidden; if not, the fetch was wasted work. Prefetching is a particular way to speculate about future data needs. section 11.1 section 11.4

The cost of a wrong prediction depends on where speculation runs. A CPU may use spare cycles and parallel execution units with little throughput penalty. Application-level speculation can consume CPU, memory, and network capacity; failed predictions may even increase latency. CPU behavior can also contribute to latency variance that software cannot directly control. section 11.4 section 2.3.2

How Latency covers it

How deep it goes
Defines early execution and examines prediction costs, without detailing CPU predictor circuitry or a runnable implementation.
Examples
Uses speculative CPU loads and branches alongside login-time prefetching of user-specific data.
What you can do after
Evaluate/understand it by weighing hidden latency against wasted resources and possible latency increases from failed predictions.
How it connects
Connects to prefetching, optimistic concurrency control, branch prediction, and latency variance.

Also explained in

Compare how 2 books explain speculative execution →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Latency. The section links point to the parts of the book it draws on.