Important: Please read the Qt Code of Conduct -

Q_UNLIKELY and compiler optimization

  • Moderators

    If I put a function call inside Q_UNLIKELY can I be sure that it won't be optimized out? Am I gaining the cache branching reduction one'd expect this way, or it's better to use a variable? E.g.

    if (Q_UNLIKELY(myfunction() != SUCCESS))
         ; // Do stuff

    or should I use:

    register int result = myfunction();
    if (Q_UNLIKELY(result != SUCCESS))
         ; // Do stuff

    Kind regards.

  • Lifetime Qt Champion


    What is the value of success ? Depending on it you can use Q_LIKELY but in any case AFAIK, it won't be optimized out.

  • Moderators

    Success is equal to zero. I'm not expecting MPI_Iprobe (the function in question) to ever return an error, unless something is very wrong with a node. However the constant is external to my application and while it's value probably won't change I can't be certain of it.

  • Lifetime Qt Champion

    That's the kind of constant that shouldn't change. Usually success = 0 and anything else (usually below 0) is an error code. Sometimes a positive number can be an information like the size of something.

    I'd go for the first version of your if in case you have doubts that SUCCESS might change

Log in to reply