Standard way to supply hints to branches

Walter Bright newshound2 at digitalmars.com
Fri Sep 13 18:53:11 UTC 2024


On 9/13/2024 4:56 AM, Timon Gehr wrote:
> Well, it is the attribute being associated with the program path being 
> ill-defined that is being criticized in that blog post. The difference is that 
> for path-associated, you are saying that a specific statement is likely or 
> unlikely to be executed, for branch-associated, you are saying in which 
> direction a specific branch is likely to go.

Ok, thanks for the explanation. The branch predictor on CPUs defaults to a 
forward branch being considered unlikely, and a backwards branch being 
considered likely.

I'm sure the CPU designers collected statistics on this before burning this into 
the branch predictor hardware.

It's a simple and easily remembered rule, and so why dmd behaves as it does.

As for an attribute, I cannot see it being viable for anything other than the 
`if`, as the undocumented menace of it being applied to anything else is 
apparent in the blog post.

Hence, if D were to support something along those lines, it would be a keyword 
such as:

```
ifrarely (i) return 0;
```

as the least ugly approach. But I've been unable to come up with an obviously 
good keyword for it. And we would need buyin from Iain and Martin.


More information about the Digitalmars-d mailing list