Re: help : likely

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

 





On 8/30/05, Rahul Iyer <idlisambar@xxxxxxxxx> wrote:
raja wrote:

> Hi,
>    In kernel code the macros likely() and unlikely() are frequently
> used.Would you please tell me wht those macros will actually perform?.
> thanking you,
> raja
>
> --
> Kernelnewbies: Help each other learn about the Linux kernel.
> Archive:       http://mail.nl.linux.org/kernelnewbies/
> FAQ:           http://kernelnewbies.org/faq/
>
>
Hi Raja,
likely() and unlikely() are directives to the compiler (gcc in this
case), used to tell it what the usual outcome of the conditional will
be. Using this knowledge, the compiler can better optimize the code.

For example, error checks/asserts do not occur too commonly... like:

if (nodeptr == NULL)
     BUG();

The above code is an example assert. for the most part, if you
programmed it correctly, nodeptr will never be NULL. You know this, so u
can help out the compiler a bit.

if (unlikely(nodeptr == NULL))
     BUG();

this will tell the compiler that the equality condition is the rare case
and to optimize the code for the common case, which is nodeptr != NULL.

Similarly, using likely(),

if (likely(nodeptr != NULL)) {
     /* do stuff */
}
else
     BUG();

Hope that helps...
ciao
-rahul

Well there have been number of mail on usage of __builtin_expect, but can any one tell how exactly the compiler use this info to optimize the mahchine code it writes. AFAIK, if .. else statement is converted into some type jmp statements, depending upon the condition is true or false. By telling the compiler that we have more probabilty to have this condition true (in case of likely) or more probabilty to having this condition false (in case of unlikely), how we are helping the compiler to arrange the corresponding jump instruction to optimize the execution of code.

It would be great if somebody can tell how compiler internally benifits from it to arrange the code in optimized manner.

-Gaurav

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           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