Standard way to supply hints to branches

Johan j at j.nl
Thu Sep 12 17:17:52 UTC 2024


On Wednesday, 11 September 2024 at 18:54:21 UTC, Walter Bright 
wrote:
> On 9/11/2024 4:18 AM, Manu wrote:
>> Obfuscating the contorting code is not the goal or a 
>> reasonable solution; we just want a mechanism in the language 
>> to take advantage of this general category of support in 
>> whatever architecture.
>
> I tend to agree, but when micro-optimizing one's code, one 
> accepts that its elegance is going to decline.
>
> There are at least 3 ways to organize the code to get what you 
> want. I won't claim they're beautiful, but they work.

FWIW, I agree with Walter that it is not worth to add a new 
special feature to the language for the problem at hand.

It is rare that you'd want to explicitly tell whether a branch is 
likely or not, and for that case the tool already exist 
(`llvm_expect` for LDC). It think it would hurt D as a whole to 
have special stuff for such a rare thing.

I do agree that it'd be good to have a common interface for all 
compilers, which can be achieved by e.g. introducing a 
`core.micro_optimization` module.
That module could collect more of such micro optimization things, 
like telling the compiler about likely function pointers or class 
types (devirtualization). [1]

Cheers,
   Johan


[1]
```
auto is_likely(alias Likely, Fptr, Args...)(Fptr fptr, Args args) 
{
     return (fptr == &Likely) ? Likely(args) : fptr(args);
}
// ...
void function() fptr = get_function_ptr();
fptr.is_likely!likely_function();
```
See 
https://johanengelen.github.io/ldc/2016/04/13/PGO-in-LDC-virtual-calls.html


More information about the Digitalmars-d mailing list