2002-01-16 03:23:21

by David Schwartz

[permalink] [raw]
Subject: likely/unlikely


By the way, has anybody actually benchmarked these macros to see if they
make any difference. I did a few quick inexact benchmarks on test code and
found that in most cases there was no difference and in some cases code was
made worse.

--
David Schwartz
<[email protected]>



2002-01-16 05:15:16

by Jeff Garzik

[permalink] [raw]
Subject: Re: likely/unlikely

David Schwartz wrote:
>
> By the way, has anybody actually benchmarked these macros to see if they
> make any difference. I did a few quick inexact benchmarks on test code and
> found that in most cases there was no difference and in some cases code was
> made worse.

likely/unlikely set the branch prediction values to 99% or 1%
respectively. If this causes the code generated to perform less
optimally than without, I'm sure the gcc guys would be -very- interested
to hear that...

--
Jeff Garzik | Alternate titles for LOTR:
Building 1024 | Fast Times at Uruk-Hai
MandrakeSoft | The Took, the Elf, His Daughter and Her Lover
| Samwise Gamgee: International Hobbit of Mystery

2002-01-16 06:01:16

by Anton Blanchard

[permalink] [raw]
Subject: Re: likely/unlikely


> likely/unlikely set the branch prediction values to 99% or 1%
> respectively. If this causes the code generated to perform less
> optimally than without, I'm sure the gcc guys would be -very- interested
> to hear that...

On some ppc64 the branch prediction is quite good and static prediction
will override the dynamic prediction. I think we avoid predicting a
branch unless we are quite sure (95%/5%).

So if likely/unlikely is overused (on more marginal conditionals) then
it could be a performance loss.

Anton

2002-01-16 06:31:44

by Craig Christophel

[permalink] [raw]
Subject: Re: likely/unlikely


> likely/unlikely set the branch prediction values to 99% or 1%


So all of the BUG() routines in the kernel would benifit greatly from this.



Craig.

2002-01-16 07:40:07

by Adrian Bunk

[permalink] [raw]
Subject: Re: likely/unlikely

On Wed, 16 Jan 2002, Craig Christophel wrote:

> > likely/unlikely set the branch prediction values to 99% or 1%
>
>
> So all of the BUG() routines in the kernel would benifit greatly from this.

In the 2.5 kernels this is done when you use the BUG_ON macro that is
defined as follows:

#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)


BTW: Note that likely/unlikely doesn't has any effect for most of us
because __builtin_expect is only available in gcc >= 2.96.


> Craig.

cu
Adrian


2002-01-16 09:14:38

by Jeff Garzik

[permalink] [raw]
Subject: Re: likely/unlikely

Anton Blanchard wrote:
>
>
> > likely/unlikely set the branch prediction values to 99% or 1%
> > respectively. If this causes the code generated to perform less
> > optimally than without, I'm sure the gcc guys would be -very- interested
> > to hear that...
>
> On some ppc64 the branch prediction is quite good and static prediction
> will override the dynamic prediction. I think we avoid predicting a
> branch unless we are quite sure (95%/5%).
>
> So if likely/unlikely is overused (on more marginal conditionals) then
> it could be a performance loss.

oh agreed... but marginal conditionals should not be getting
likely()/unlikely() as you are then lying to the compiler about the true
branch predictability...

Jeff


--
Jeff Garzik | Alternate titles for LOTR:
Building 1024 | Fast Times at Uruk-Hai
MandrakeSoft | The Took, the Elf, His Daughter and Her Lover
| Samwise Gamgee: International Hobbit of Mystery

2002-01-16 09:17:28

by Jeff Garzik

[permalink] [raw]
Subject: Re: likely/unlikely

Craig Christophel wrote:
>
> > likely/unlikely set the branch prediction values to 99% or 1%
>
> So all of the BUG() routines in the kernel would benifit greatly from this.


It's likely :) I would put one unlikely() in the definition of BUG_ON,
rather rather touching all the code that calls BUG(), though.

Jeff



--
Jeff Garzik | Alternate titles for LOTR:
Building 1024 | Fast Times at Uruk-Hai
MandrakeSoft | The Took, the Elf, His Daughter and Her Lover
| Samwise Gamgee: International Hobbit of Mystery

2002-01-16 09:30:38

by Jens Axboe

[permalink] [raw]
Subject: Re: likely/unlikely

On Wed, Jan 16 2002, Jeff Garzik wrote:
> Craig Christophel wrote:
> >
> > > likely/unlikely set the branch prediction values to 99% or 1%
> >
> > So all of the BUG() routines in the kernel would benifit greatly from this.
>
>
> It's likely :) I would put one unlikely() in the definition of BUG_ON,
> rather rather touching all the code that calls BUG(), though.

BUG_ON has had unlikely since the very beginning :-)

--
Jens Axboe