Likelihood of if statement and case

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Sun Sep 22 15:31:08 UTC 2024


On 22/09/2024 9:49 PM, IchorDev wrote:
> On Wednesday, 11 September 2024 at 09:26:16 UTC, Richard (Rikki) Andrew 
> Cattermole wrote:
>> ```d
>> switch(e) {
>>     /*@likely*/
>>     case E.unlikely1:
>>         break;
>>     default:
>>         break;
>> }
>>
>> if (e.unlikely1) /*@likely*/ {
>> } else {
>> }
>> ```
>>
>> ``@likely`` would be defined in core.attributes. It requires minimal 
>> changes, but instead it requires us to explicitly define this behavior 
>> of priorities and likelihood.
> 
> How would one emulate this?
> ```d
> void x(int a, uint b){
>    if(a < 0) @unlikely return;
>    if(b == uint.max) @unlikely return;
>    //do stuff
> }
> ```
> Not being able to mark an if-return as unlikely is one of the major 
> problems with the current design, because the if-return pattern avoids 
> issues with really deep nesting that would occur otherwise.

Why would you need to annotate it?

It is the default for the true path.

The ``@likely`` annotation is _swapping_ the likelihoods of the two paths.

Note: compilers are free to add other attributes once UDA's are turned on.


More information about the dip.ideas mailing list