Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any performance test results for usage of likely/unlikely hints?

gcc features likely/unlikely hints that help the compiler to generate machine code with better branch prediction.

Is there any data on how proper usage or failure to use those hints affects performance of real code on some real systems?

like image 936
sharptooth Avatar asked Sep 30 '11 08:09

sharptooth


2 Answers

The question differs, but Peter Cordes's answer on this question gives a clear hint ;) . Modern CPU's ignore static hints and use dynamic branch prediction.

like image 107
MSalters Avatar answered Nov 15 '22 20:11

MSalters


I don't know of any thorough analysis of such particular hints. In any case, it would be extremely CPU-specific. In general, if you are sure about the likelyhood (e.g., > 90%) then it is probably worthwhile to add such annotations, although improvements will vary a lot with the specific use case.

Modern Desktop CPUs tend to have very good branch prediction. If your code is on a hot path anyway, the dynamic branch predictor will quickly figure out that the branch is biased on its own. Such hints are mainly useful to help the static predictor which kicks in if no dynamic branch information is available.

On x86, the static predictor predicts forward branches not to be taken and backward branches to be taken (since they usually indicate loops). The compiler will therefore adjust static code layout to match the predictions. (This may also help putting the hot path on adjacent cache lines, which may help further.)

On PPC, some jump instructions have a bit to predict their likelyhood. I don't know if the compiler will rearrange code, too.

I don't know how ARM CPUs predict branches. As a low-power device it may have less sophisticated branch prediction and static prediction could have more impact.

like image 34
nominolo Avatar answered Nov 15 '22 19:11

nominolo