Likelihood of if statement and case
IchorDev
zxinsworld at gmail.com
Sun Sep 22 09:49:38 UTC 2024
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.
More information about the dip.ideas
mailing list