Re: [HACKERS] Do we need the gcc feature "__builtin_expect" topromote the branches prediction?

Поиск
Список
Период
Сортировка
От Craig Ringer
Тема Re: [HACKERS] Do we need the gcc feature "__builtin_expect" topromote the branches prediction?
Дата
Msg-id CAMsr+YGW0UZyoOP+fCHb43h=DD8WCXNoLJGnua4ADZStt44jpA@mail.gmail.com
обсуждение исходный текст
Ответ на [HACKERS] Do we need the gcc feature "__builtin_expect" to promote the branches prediction?  (Hao Lee <mixtrue@gmail.com>)
Ответы Re: [HACKERS] Do we need the gcc feature "__builtin_expect" topromote the branches prediction?  (Julien Rouhaud <julien.rouhaud@dalibo.com>)
Список pgsql-hackers


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.

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Marko Tiikkaja
Дата:
Сообщение: [HACKERS] PG10 transition tables, wCTEs and multiple operations on the same table
Следующее
От: Dmitriy Sarafannikov
Дата:
Сообщение: Re: [HACKERS] Broken hint bits (freeze)