Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't likely/unlikely show performance improvements?

I have many validation checks in the code where program is crashed if any check gets failed. So all the checks are more unlikely.

if( (msg = newMsg()) == (void *)0 )//this is more unlikely
{
    panic()//crash
}

So I have used the macro unlikely which hints compiler in branch prediction. But I have seen no improvement with this(I have some performance tests). I am using gcc4.6.3.

Why is there no improvement ? Is it because there are no else case for this? Should I use any optimization flag while building my application?

like image 710
spoorthi sreedhara Avatar asked Nov 09 '22 00:11

spoorthi sreedhara


1 Answers

Should I use any optimization flag while building my application?

Absolutely! Even the optimizations turned at the lowest level, -O1 for GCC/clang/icc, are likely to outperform most of your optimization efforts. For free essentially, so why not?

I am using gcc4.6.3.

GCC 4.6 is old. You should consider working with modern tools, unless you're constrained otherwise.

But I have seen no improvement with this(I have some performance tests).

You haven't seen visible performance improvements, which is very common when dealing with micro-optimizations like those. Unfortunately, achieving visible improvements is not very easy with today's hardware: this is because we have got faster (unbelievably faster) components than they used to be. So saving up cycles is not as sensible as it used to be.

It's though worth noticing that sequential micro-optimizations can still make your code much faster, as in tight loops. Avoiding stalls, branch mispredictions, maximizing cache use do make a difference when handling chunks of data. And SO's most voted question shows clearly that.

It's even stated on the GCC manual:

— Built-in Function: long __builtin_expect (long exp, long c)
You may use __builtin_expect to provide the compiler with branch prediction information. In general, you should prefer to use actual profile feedback for this (-fprofile-arcs), as programmers are notoriously bad at predicting how their programs actually perform. However, there are applications in which this data is hard to collect.

(emphasis mine)

like image 155
edmz Avatar answered Nov 15 '22 07:11

edmz