On Sat, 31 Aug 2024 at 17:16, Dom DiSc via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
On Friday, 30 August 2024 at 02:54:28 UTC, Manu wrote:
> On Fri, 30 Aug 2024 at 04:32, Walter Bright via Digitalmars-d <
> digitalmars-d@puremagic.com> wrote:
>
>> On 8/28/2024 2:45 AM, Manu wrote:
>> > Here's one that I just wrote a short while ago:
>> > https://gist.github.com/TurkeyMan/0e49da245cc0086f852ac18deed21a9c
>>
>>
>> ```
>> if (data.length < 4) // unlikely
>>      return 0;
>> ```
>>
>> replace with:
>>
>> ```
>> if (data.length < 4)
>>      goto Lreturn0;
>> ```
>>
>
> How is that any different? The branch prediction hasn't changed.

I already asked this question.
He said a single break or goto is NOT considered the hot branch
by the compiler.

The goto is not a branch at all. The if() is the branch... and it still predicts the pessimistic case incorrectly.
Replacing the return with a goto hasn't changed the control flow in any way; goto and return are equivalent; they are not branches.
 
But I don't like this, because it's an implementation detail that
every compiler may implement or not, and it's not documented
anywhere.
Maybe if the documentation would clearly state at a prominent
point that a single break or goto has to be considered the cold
path by the compiler, but a function call is to be considered the
hot path (and all related queries about branch priorization link
there), then I would consider this a solution. But is it likely
this will happen?