Daniel Lemire's blog

, 3 min read

Adding a (predictable) branch to existing code can increase branch mispredictions

5 thoughts on “Adding a (predictable) branch to existing code can increase branch mispredictions”

  1. Jesse Jenkins says:

    How did you measure the predicts/mispredicts?

    1. Using the processor’s counters.

  2. Mike G. says:

    Wow that’s a low baseline mispredict rate on Intel.

    Does that rng function result in an easily predictable even/odd pattern?

    1. What matters is that these are always the same pseudo-random integers from run to run.

  3. Wilco says:

    Yes this is kind of obvious since adding the 3rd branch adds useless entries in the branch history, reducing its effective length and thus the number of unique patterns it can remember.

    Unrolling it 4x or 8x will have the opposite effect since it removes the always taken loop branch which has the same effect.