Optimizing a branch for a known more-common path

前端 未结 4 1126
深忆病人
深忆病人 2020-12-06 07:55

Please consider the following piece of code:

void error_handling();
bool method_impl();

bool method()
{
    const bool res = method_impl();
    if (res == f         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 08:19

    You could suggest the compiler that the method_impl() will return true:

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

    This will work in GCC.

提交回复
热议问题