Speculative execution

From Wikipedia, the free encyclopedia

In computer science, speculative execution is the execution of code the result of which may not be needed. In the context of functional programming, the term "speculative evaluation" is used instead.

Generally, statements and definitions in a program can be divided into three types:

  • things which must be run and are mandatory
  • things which do not need to be run because they are irrelevant
  • statements which cannot be proven to be in either of the first two groups

The first group does not benefit from speculative execution because they need to run anyway. The second group can be quietly discarded much like lazy evaluation would discard them. The third group is the target of speculative evaluation, as they can be run concurrently with the mandatory computations until they are needed or shown to be of the second group; this concurrency means that speculative execution can be parallelized.

Speculative execution is a performance optimization. It is only useful when early execution consumes less time and space than later execution would, and the savings are enough to compensate, in the long run, for the possible wasted effort of computing a value which is never used.

Modern pipelined microprocessors use speculative execution to reduce the cost of conditional branch instructions. When a conditional branch instruction is encountered, the processor guesses which way the branch is most likely to go (this is called branch prediction), and immediately starts executing instructions from that point. If the guess later proves to be incorrect, all computation past the branch point is discarded. The early execution is relatively cheap because the pipeline stages involved would otherwise lie dormant until the next instruction was known. However, wasted instructions consume CPU cycles that could have otherwise delivered performance, and on a laptop, those cycles consume batteries. There is always a penalty for a mispredicted branch. Many microprocessors (such as the Intel Pentium II and successors) have a conditional move instruction. This is an operation to move data, usually, between a register and memory, if a condition is met. There is no branching any more, and the misprediction penalty is less.

Though it's seldom referred to as such, eager execution is also a form of speculative execution (although the situation is complicated by the presence of side effects). Early execution is often cheaper because values needed for the computation are likely to be available on the stack and need not be stored and later retrieved from the heap. It can also be substantially more expensive, for instance in the case of generating the list of integers from 1 to 1,000,000. Programmers writing code in a strict programming language avoid these cases by using explicit laziness or by circumlocution (which can become very elaborate).

Lazy evaluation does not speculate. The incorporation of speculative execution into implementations of the Haskell programming language is a current research topic. Eager Haskell is designed around the idea of speculative execution. Recent versions of GHC support a kind of speculative execution called optimistic execution.[1]

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.