Perspective of prediction of portable branches in C ++

Branch forecasts have been reviewed a couple of times at StackOverflow. However, I deliberately did not find the answer to what I am looking for. During the optimization phase, I need to avoid mispredicting the industry. And I have a number of checks that I need to do. It looks like this:

if(!successCondition)
    { throw Something(); }

Of course, in the normal expected workflow, which occurs in most cases, we do not throw an exception, so we do not introduce if.

I know that in the general if / else paradigm we can hint at the compiler by putting the most probable branch in if, and the less probable branch in else ( Primary branch prediction of hints ). But I don't want (due to readability) the ifs chain:

if(successCondition)
    { whatever(); }
else
    { throw Something(); }

This way, I understand that the compiler will by default approve the if entry, and I will get the branch prediction incorrect.

I know that gcc has a specific function for optimizing code that is invoked with an unlikely Linux kernel ( Branch-based programming ). But it is not portable, and I need my code.

Is there a way to have the correct branch prediction in C ++ that remains portable?

+4
source share
1 answer

Most likely, C ++ 20 is likely to have attributes for this purpose. (The draft committee is still under consideration.)

+6
source

Source: https://habr.com/ru/post/1691808/


All Articles