Optimizing a branch for a known more-common path

Viewed 3868

Please consider the following piece of code:

void error_handling();
bool method_impl();

bool method()
{
    const bool res = method_impl();
    if (res == false) {
        error_handling();
        return false;
    }
    return true;
}

I know method_impl() will return true 99.999% (yes, three decimal places) of the time, but my compiler doesn't. method() is partially critical in term of time-consumption.

  1. Should I rewrite method() (and make it less readable) to ensure a jump may only occur when method_impl() returns false? If yes, how?
  2. Should I let the compiler do the work for me?
  3. Should I let the branch prediction of my CPU do the work for me?
4 Answers
Related