Fw:Re: benefits to likely() and unlikely()?

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

 



 Hi,
    When the cpu executing the current instruct, the next instruct it should execute has already predicted and  read  into  the IP register(it cache the next instruct).

    When it encounter conditional branch. Processor(CPU) try to predict which branch will be taken. If the prediction is right, it improve the performance. Otherwise, the processor has wasted its time . It have to re-read the right baranch.

    But the processor doesn't know whether branch will be taken or not, so , the likely() and unlikely() condition tell him which branch it should chose, and it does  improve the performance, generally.


    Hope it helps.

    Best regards.
 
 
 
---------- 转发邮件信息 ----------
发件人:""Robert P. J. Day" <rpjday@xxxxxxxxxxxxxx>"
发送日期:2008-03-30 17:17:42
收件人:"Erik Mouw" <mouw@xxxxxxxxxxxx>
抄送:"Kernel Newbies" <kernelnewbies@xxxxxxxxxxxx>
 
主题: Re: benefits to likely() and unlikely()?
On Sun, 30 Mar 2008, Erik Mouw wrote:

> On Sat, Mar 29, 2008 at 04:03:18AM -0400, Robert P. J. Day wrote:
> >   is there somewhere an actual quantification (is that a word?) to
> > the benefits of likely() and unlikely() in the kernel code?  i've
> > always been curious about what difference those constructs made.
> > thanks.
>
> They are macros around __builtin_expect(), which can be used to
> provide the compiler with branch prediction information. In the
> kernel, you see likely()/unlikely() usually used in error handling:
> most of the times you don't get an error, so tell the compiler to
> lay out the code in such a way that the error handling block becomes
> a branch and the normal code flows just straight. Something like:
>
>
> 	if(unlikely(ptr == NULL)) {
> 		printk(KERN_EMERG "AARGH\n");
> 		panic();
> 	}
>
> 	foo(ptr);

oh, i realize what they *represent*.  what i was curious about was the
actual numerical *benefit*.  as in, performance analysis and how much
of a difference it really makes.  did someone do any benchmarking?

rday
--

========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry:
    Have classroom, will lecture.

http://crashcourse.ca                          Waterloo, Ontario, CANADA
========================================================================

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




中 国 最 强 网 游 --- 网 易 梦 幻 西 游 ,166 万 玩 家 同 时 在 线

[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