Hi Egor,

On Wed, Apr 15, 2026 at 06:07:54PM +0000, Egor Shestakov wrote:
> Hi List!
> 
> I found a code duplication in run_tasks_from_lists(), when a task's
> process() callback runs. Initially I wanted just eliminate the duplication,
> but discovered there was a branch prediction optimization (see f0c531ab5
> (MEDIUM: tasks: implement a lockless scheduler for single-thread usage,
> 2017-11-05):
> 
>     /* This is an optimisation to help the processor's branch
>      * predictor take this most common call.
>      */
>     if (likely(t->process == process_stream))
>       t = process_stream(t);
>     else
>       t = t->process(t);
> 
> Along the way, this became into this:
> 
>     if (process == process_stream)
>       t = EXEC_CTX_WITH_RET(EXEC_CTX_MAKE(TH_EX_CTX_TASK, process_stream),
>                             process_stream(t, ctx, state));
>     else
>       t = EXEC_CTX_WITH_RET(EXEC_CTX_MAKE(TH_EX_CTX_TASK, process),
>                             process(t, ctx, state));
> 
> Should this optimization be restored? If not, maybe it's better to just
> remove the code duplication?

I remember testing it again around 2 years ago and it was still worth
it. It's due to branch prediction units in CPUs which are extremely
effective and allow to prefetch instructions from process_stream()
before the branch is decided, and since it's by far the most called
function when under high HTTP loads, the few tens of cycles saved
there are a net overall gain.

We could recheck at some point though.

Regards,
Willy


Reply via email to