Thread: Linux likely() unlikely() for PostgreSQL

Linux likely() unlikely() for PostgreSQL

From
wenhui qiu
Date:
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

Thanks

Re: Linux likely() unlikely() for PostgreSQL

From
Matthias van de Meent
Date:
On Sun, 30 Jun 2024, 15:56 wenhui qiu, <qiuwenhuifx@gmail.com> wrote:
>
> Hi Hackers
>    When I saw this document:https://en.wikipedia.org/wiki/Branch_predictor, I thought of Linux likely() vs unlikely()
andthus 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/)