The Lost Feed

🔬Weird Science

The Strange Story of CPU Branch Prediction

How computers learned to guess your next move. A deep dive into the clever tech behind faster processors.

2 views·6 min read·Jun 15, 2026
CPU branch prediction evolution over years (2017)

Imagine a chef trying to cook a complex meal. They need to chop onions, then stir sauce, then add spices. But what if they could start chopping the spices *while

  • stirring the sauce, just in case that's the next step? That’s kind of what branch prediction is for computers.

Computers follow instructions like a recipe. But sometimes, the recipe has a fork in the road. Should we go left or right? The computer has to stop and wait for the decision. This waiting slows things down. Branch prediction is the computer’s way of making a smart guess about which way to go, so it doesn’t have to stop.

Why Waiting Is Bad For Computers

Modern computer brains, called processors, are incredibly fast. They can do billions of tasks every second. But they work best when they are always busy. When a processor has to wait for a decision, like which path to take in a program, it’s like a race car driver stopping at a red light. All that power is wasted.

This waiting happens a lot with something called “conditional branches.” These are points in a computer program where the next step depends on whether something is true or false. For example, “If the number is bigger than 10, do this. Otherwise, do that.” The processor doesn’t know which “this” or “that” to do until it checks the number.

The Early Guesses: Simple Strategies

Early processors had very simple ways to guess. One of the first ideas was just to always guess the same way. For example, always assume the condition will be false. This is like our chef always assuming they’ll add spices next, even if sometimes they need to add salt first.

This simple guess worked okay for some programs. But many programs have sections that repeat, or have lots of different choices. Always guessing one way meant the computer was wrong a lot. When it was wrong, it had to throw away all the work it did based on the wrong guess and start over. This was a big performance killer.

Another early idea was to look at what happened last time. If the program took the "true" path last time, guess "true" again. This was a bit better, but still not great. What if the program’s needs changed? It was like our chef only remembering the last meal they cooked, not what’s needed for the current one.

Getting Smarter: Remembering More History

To improve, processors started remembering more about what happened before. Instead of just looking at the very last decision, they looked at a short history of recent decisions. This is called a history buffer. Think of it like our chef remembering the last three steps, not just the last one.

This history buffer allowed for more complex guessing patterns. If a program often went "true, true, false, true, true, false," the processor could learn this pattern. It could then make a much better guess about what to do next. This was a big step forward because it meant fewer wrong guesses and less wasted time.

The Two-Bit Predictor: A Common Improvement

One popular way to use this history was with a "two-bit predictor." For each branch point, the processor kept a little counter. This counter kept track of how often the branch was taken. If the counter was high, it guessed "taken." If it was low, it guessed "not taken."

This two-bit system was clever because it required two wrong guesses in a row to flip the prediction. This meant it wouldn't get confused by a single unusual step. It needed a consistent pattern to change its mind. This made the predictions much more stable and accurate for most programs.

Learning from the Future: More Advanced Techniques

As programs got more complicated, even remembering recent history wasn't always enough. Developers came up with ways to look even further back, or to use different kinds of history.

One idea was to use the *address

  • of the instruction itself to help predict. Different parts of a program might have different branching patterns. So, instead of just remembering "true, false," the processor might remember "for *this specific section

  • of code, it usually goes true." This is like our chef knowing that recipes for desserts often have more sugar than recipes for main courses.

Another big idea was called a "gshare" predictor. This combined the history of recent branches with the address of the current branch. It was like our chef considering both the type of dish *and

  • what they just did in the last few minutes to decide the next spice.

These advanced predictors were able to achieve very high accuracy, sometimes over 90%. This means the processor was guessing right almost all the time.

The

Role of the Compiler

It’s not just the processor hardware that helps. The software that writes the computer instructions, called the compiler, also plays a role. Compilers can try to arrange the program's instructions in a way that makes them easier for the processor to predict.

For example, if a part of the code is very likely to be used, the compiler might place those instructions closer together. This can help the processor's prediction logic. It's like organizing your kitchen so the most-used spices are right next to the stove.

Some compilers can even add special hints to the program. These hints tell the processor, "This branch is probably going to go this way." The processor can then use these hints to make an even better guess. This collaboration between hardware and software is key to getting the best performance.

Why This Still Matters Today

Even though this technology has been around for decades, the basic ideas of branch prediction are still fundamental to how modern CPUs work. Processors today are incredibly complex, but the need to avoid waiting is still the same.

As processors get faster and try to do more things at once (like having multiple "cores"), the challenge of keeping everything fed with instructions becomes even bigger. Good branch prediction is essential to make sure all those cores are busy and not stuck waiting for decisions.

Think about playing a fast-paced video game. If the computer can't predict what you're going to do next in the game's code, the game will feel laggy and slow. Branch prediction is one of the hidden heroes that makes your computer feel responsive and fast, allowing it to keep up with our demands.

It’s a constant race to make better guesses. The more accurately a processor can predict the future of a program’s execution, the faster and more efficient it will be. This clever guessing game is a core reason why our computers have become so powerful.

How does this make you feel?

Comments

0/2000

Loading comments...