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

From: Andres Freund <andres(at)anarazel(dot)de>
To: Hao Lee <mixtrue(at)gmail(dot)com>
Cc: PostgreSQL hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Do we need the gcc feature "__builtin_expect" to promote the branches prediction?
Date: 2017-06-02 15:32:17
Message-ID: 20170602153217.edi2xdvzck42sgcu@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2017-06-02 15:45:32 Re: [HACKERS] [PATCH] relocation truncated to fit: citus build failure on s390x
Previous Message Alvaro Herrera 2017-06-02 15:08:46 Re: Re: [GSOC 17] Eliminate O(N^2) scaling from rw-conflict tracking in serializable transactions