Re: likely() and unlikely()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sunday 26. July 2009 14.53.24 Leena M. wrote:
> Hi All,
>
> What do likely() and unlikely()  mean ?

They are compiler directivs indicationg the most likely outcome of a test so 
that the compiler can optimize the branching of that particular case. If you 
know that the test will be true 95% of the time, you can do a lot of code 
optimization based on that.

In other words, when you have a branch, you either follow the code-stream 
(keep using the instructions in the pipeline) or you branch to an alternate 
stream. The latter will force the CPU to empty the pipeline and fetch new 
instructions from memory. This is expensive in the long run.

> I see that it says :
>
> linux-2.6.30.2\linux-2.6.30.2\include\linux\compiler.h"
>
> /*
>  * Using __builtin_constant_p(x) to ignore cases where the return
>  * value is always the same.  This idea is taken from a similar patch
>  * written by Daniel Walker.
>  */
> # ifndef likely
> #  define likely(x)    (__builtin_constant_p(x) ? !!(x) :
> __branch_check__(x, 1))
> # endif
> # ifndef unlikely
> #  define unlikely(x)    (__builtin_constant_p(x) ? !!(x) :
> __branch_check__(x, 0))
> # endif
>
> but can't easliy recognize what is actually intended when likely() and
> unlikely() is used ??
>
> for instance :
> here
> if (likely((atomic_read(&files->count) == 1)))

In almost every case, the the atomic_read will eval to 1, and you want the 
code to be laid out to avoid stalling the pipeline.

Remember: Optimize for the common case.

-- 
     henrik

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux