Re: Linux likely() unlikely() for PostgreSQL

From: Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>
To: wenhui qiu <qiuwenhuifx(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Linux likely() unlikely() for PostgreSQL
Date: 2024-06-30 14:47:02
Message-ID: CAEze2Whn=6XVCqMNeka7C3VYXua0KH56h9S2=9Ps42hdyXwfmQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, 30 Jun 2024, 15:56 wenhui qiu, <qiuwenhuifx(at)gmail(dot)com> wrote:
>
> Hi Hackers
> When I saw this document:https://en.wikipedia.org/wiki/Branch_predictor, I thought of Linux likely() vs unlikely() and thus thought that there are some code segments in src/backend/executor/execMain.c that can be optimized.
> For example :
> if (ExecutorStart_hook)
> (*ExecutorStart_hook) (queryDesc, eflags);
> else
> standard_ExecutorStart(queryDesc, eflags);
> }
> void
> ExecutorRun(QueryDesc *queryDesc,
> ScanDirection direction, uint64 count,
> bool execute_once)
> {
> if (ExecutorRun_hook)
> (*ExecutorRun_hook) (queryDesc, direction, count, execute_once);
> else
> standard_ExecutorRun(queryDesc, direction, count, execute_once);
> }
> ###
> if (unlikely(ExecutorRun_hook)),
>
> I hope to get guidance from community experts,Many thanks

While hooks are generally not installed by default, I would advise
against marking the hooks as unlikely, as that would unfairly penalize
the performance of extensions that do utilise this hook (or hooks in
general when applied to all hooks).

If this code is hot enough, then CPU branch prediction will likely
correctly predict this branch correctly after a small amount of time
(as hook null-ness is generally approximately constant for the
duration of a backend lifetime); while if this code is cold, the
benefit is not worth the additional binary size overhead of the
out-of-lined code section.

Kind regards,

Matthias van de Meent
Neon (https://neon.tech/)

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Joel Jacobson 2024-06-30 15:17:44 Re: Optimize numeric.c mul_var() using the Karatsuba algorithm
Previous Message wenhui qiu 2024-06-30 13:55:39 Linux likely() unlikely() for PostgreSQL