Performance Analysis

Performance counters reveal very frequent branch mispredictions. Which is a suitable remedy?

  1. AAdd more branches inside the loop
  2. BSort data so branch outcomes become predictable, or eliminate branches with conditional selects or arithmetic
  3. CReduce cache capacity
Show answer and explanation

Answer: B. Sort data so branch outcomes become predictable, or eliminate branches with conditional selects or arithmetic

On a branch misprediction, the CPU discards speculatively executed instructions and restarts, losing a dozen or more cycles each time on a deeply pipelined CPU. With random data, the outcome of a branch such as "if the value is at least 128" cannot be predicted. Sorting the data first makes outcomes uniform and predictable, while conditional-select instructions or bit operations remove the branch entirely. Compilers can also be encouraged to make such transformations.

  • A. That could increase mispredictions further.
  • B. Correct. Make branches predictable or remove them.
  • C. It is unrelated to prediction and would hurt performance.