Do we need the gcc feature "__builtin_expect" to promote the branches prediction?

Started by Hao Leeover 8 years ago7 messages
#1Hao Lee
mixtrue@gmail.com

Hi all,
There is a lot of "if statement" in system, and GCC provides a
feature,"__builtin_expect", which let compilers know which branch is
mostly run. as we known, miss-prediction will lead the performance
lost(because the CPU will thrown away some instructions, and re-fetch some
new instructions). so that we can tell GCC how produce more efficient code.
for example as following.
It will gain performance promotion i think. As i know, the in Linux kernel,
this feature is also applied already.

#define likely(cond) __builtin_expect(cond,true)
#define unlikely(cond) __builtin_expect(cond,false)

if (likely(cond)) {
//most likely run.
xxxx
} else //otherwise.
{
xxxx
}

Best Regards.

Hom.

#2Craig Ringer
craig.ringer@2ndquadrant.com
In reply to: Hao Lee (#1)
Re: Do we need the gcc feature "__builtin_expect" to promote the branches prediction?

On 2 Jun. 2017 16:42, "Hao Lee" <mixtrue@gmail.com> wrote:

Hi all,
There is a lot of "if statement" in system, and GCC provides a
feature,"__builtin_expect", which let compilers know which branch is
mostly run.

Compilers and CPUs are really good at guessing this.

Humans are wrong about it more than we'd like too.

It's surprisingly rarely s good idea to use branch prediction hints.

See the vsrious Linux kernel discussions about this.

If you find concrete sites of frequent or costly branch mis-prediction
please point them out, with benchmarks.

. as we known, miss-prediction will lead the performance lost(because the
CPU will thrown away some instructions, and re-fetch some new
instructions). so that we can tell GCC how produce more efficient code. for
example as following.
It will gain performance promotion i think. As i know, the in Linux kernel,
this feature is also applied already.

#define likely(cond) __builtin_expect(cond,true)
#define unlikely(cond) __builtin_expect(cond,false)

if (likely(cond)) {
//most likely run.
xxxx
} else //otherwise.
{
xxxx
}

Best Regards.

Hom.

#3Julien Rouhaud
julien.rouhaud@dalibo.com
In reply to: Craig Ringer (#2)
Re: Do we need the gcc feature "__builtin_expect" to promote the branches prediction?

On 02/06/2017 12:50, Craig Ringer wrote:

On 2 Jun. 2017 16:42, "Hao Lee" <mixtrue@gmail.com
<mailto:mixtrue@gmail.com>> wrote:

Hi all,
There is a lot of "if statement" in system, and GCC provides
a feature,"__builtin_expect", which let compilers know which branch
is mostly run.

Compilers and CPUs are really good at guessing this.

Humans are wrong about it more than we'd like too.

+1

It's surprisingly rarely s good idea to use branch prediction hints.

See the vsrious Linux kernel discussions about this.

If you find concrete sites of frequent or costly branch mis-prediction
please point them out, with benchmarks.

And also see this thread:
/messages/by-id/CAKJS1f8yqRW3qx2CO9r4bqqvA2Vx68=3awbh8CJWTP9zXeoHMw@mail.gmail.com

BTW Andres added support for likely/unlikely in aa3ca5e3dd6, but its
usage is still really limited.

--
Julien Rouhaud
http://dalibo.com - http://dalibo.org

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#4Hao Lee
mixtrue@gmail.com
In reply to: Julien Rouhaud (#3)
Re: Do we need the gcc feature "__builtin_expect" to promote the branches prediction?

Okay. Thanks

On Fri, Jun 2, 2017 at 7:27 PM, Julien Rouhaud <julien.rouhaud@dalibo.com>
wrote:

Show quoted text

On 02/06/2017 12:50, Craig Ringer wrote:

On 2 Jun. 2017 16:42, "Hao Lee" <mixtrue@gmail.com
<mailto:mixtrue@gmail.com>> wrote:

Hi all,
There is a lot of "if statement" in system, and GCC provides
a feature,"__builtin_expect", which let compilers know which branch
is mostly run.

Compilers and CPUs are really good at guessing this.

Humans are wrong about it more than we'd like too.

+1

It's surprisingly rarely s good idea to use branch prediction hints.

See the vsrious Linux kernel discussions about this.

If you find concrete sites of frequent or costly branch mis-prediction
please point them out, with benchmarks.

And also see this thread:
/messages/by-id/CAKJS1f8yqRW3qx2CO9r4bqqvA2Vx6
8=3awbh8CJWTP9zXeoHMw@mail.gmail.com

BTW Andres added support for likely/unlikely in aa3ca5e3dd6, but its
usage is still really limited.

--
Julien Rouhaud
http://dalibo.com - http://dalibo.org

#5Andres Freund
andres@anarazel.de
In reply to: Hao Lee (#1)
Re: Do we need the gcc feature "__builtin_expect" to promote the branches prediction?

Hi,

On 2017-06-02 16:40:56 +0800, Hao Lee wrote:

Hi all,
There is a lot of "if statement" in system, and GCC provides a
feature,"__builtin_expect", which let compilers know which branch is
mostly run. as we known, miss-prediction will lead the performance
lost(because the CPU will thrown away some instructions, and re-fetch some
new instructions). so that we can tell GCC how produce more efficient code.
for example as following.
It will gain performance promotion i think. As i know, the in Linux kernel,
this feature is also applied already.

#define likely(cond) __builtin_expect(cond,true)
#define unlikely(cond) __builtin_expect(cond,false)

if (likely(cond)) {
//most likely run.
xxxx
} else //otherwise.
{
xxxx
}

We already do this in a few cases, that are performance critical enough
to matter. But in most cases the CPUs branch predictor does a good
enough job on its own.

- Andres

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#6Hao Lee
mixtrue@gmail.com
In reply to: Andres Freund (#5)
Re: Do we need the gcc feature "__builtin_expect" to promote the branches prediction?

and another things come into my mind, in some scenario, as we know that the
native library is not the most effective way to do that, such as,
allocation a large amount of memories by using "alloc()"... and
"memmove()", so on. As the SIMD instruction became the standard in CPU,
therefore, we can that to do something more effectively. for example, using
SIMD to impl 'alloc',"memmove" and so on, or the other database operations.
and it seems that there some one have done this job.

https://pdfs.semanticscholar.org/eff8/5ca6394eb2ed1e48fe48aa73d34d5a14760c.pdf

On Fri, Jun 2, 2017 at 11:32 PM, Andres Freund <andres@anarazel.de> wrote:

Show quoted text

Hi,

On 2017-06-02 16:40:56 +0800, Hao Lee wrote:

Hi all,
There is a lot of "if statement" in system, and GCC provides a
feature,"__builtin_expect", which let compilers know which branch is
mostly run. as we known, miss-prediction will lead the performance
lost(because the CPU will thrown away some instructions, and re-fetch

some

new instructions). so that we can tell GCC how produce more efficient

code.

for example as following.
It will gain performance promotion i think. As i know, the in Linux

kernel,

this feature is also applied already.

#define likely(cond) __builtin_expect(cond,true)
#define unlikely(cond) __builtin_expect(cond,false)

if (likely(cond)) {
//most likely run.
xxxx
} else //otherwise.
{
xxxx
}

We already do this in a few cases, that are performance critical enough
to matter. But in most cases the CPUs branch predictor does a good
enough job on its own.

- Andres

#7Craig Ringer
craig@2ndquadrant.com
In reply to: Hao Lee (#6)
Re: Do we need the gcc feature "__builtin_expect" to promote the branches prediction?

On 6 June 2017 at 10:44, Hao Lee <mixtrue@gmail.com> wrote:

and another things come into my mind, in some scenario, as we know that the
native library is not the most effective way to do that, such as, allocation
a large amount of memories by using "alloc()"... and "memmove()", so on. As
the SIMD instruction became the standard in CPU, therefore, we can that to
do something more effectively. for example, using SIMD to impl
'alloc',"memmove" and so on, or the other database operations. and it seems
that there some one have done this job.

Compilers are already pretty good at this.

https://en.wikipedia.org/wiki/Program_optimization

--
Craig Ringer http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers