<div dir="ltr"><div dir="ltr">On Thu, 12 Sept 2024 at 18:21, Johan via Digitalmars-d <<a href="mailto:digitalmars-d@puremagic.com">digitalmars-d@puremagic.com</a>> wrote:</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Wednesday, 11 September 2024 at 18:54:21 UTC, Walter Bright <br>
wrote:<br>
> On 9/11/2024 4:18 AM, Manu wrote:<br>
>> Obfuscating the contorting code is not the goal or a <br>
>> reasonable solution; we just want a mechanism in the language <br>
>> to take advantage of this general category of support in <br>
>> whatever architecture.<br>
><br>
> I tend to agree, but when micro-optimizing one's code, one <br>
> accepts that its elegance is going to decline.<br>
><br>
> There are at least 3 ways to organize the code to get what you <br>
> want. I won't claim they're beautiful, but they work.<br>
<br>
FWIW, I agree with Walter that it is not worth to add a new <br>
special feature to the language for the problem at hand.<br>
<br>
It is rare that you'd want to explicitly tell whether a branch is <br>
likely or not, and for that case the tool already exist <br>
(`llvm_expect` for LDC).</blockquote><div><br></div><div>
<div>expect() statements are not a good time.<br></div><div>And something portable needs to be added anyway; so I'd seriously suggest not using expect as an API for this.</div><div><br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">It think it would hurt D as a whole to <br>
have special stuff for such a rare thing.<br></blockquote><div><br></div><div>How?</div><div>And it's not 'rare'; it's 'niche'. For a microcontroller with no branch prediction, it's common and essential.</div><div>It's literally unworkable to write code for the platform without this tool; you can't have branches constantly mispredicting.<br></div><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I do agree that it'd be good to have a common interface for all <br>
compilers, which can be achieved by e.g. introducing a <br>
`core.micro_optimization` module.<br>
That module could collect more of such micro optimization things, <br>
like telling the compiler about likely function pointers or class <br>
types (devirtualization). [1]<br>
<br>
Cheers,<br>
Johan<br>
<br>
<br>
[1]<br>
```<br>
auto is_likely(alias Likely, Fptr, Args...)(Fptr fptr, Args args) <br>
{<br>
return (fptr == &Likely) ? Likely(args) : fptr(args);<br>
}<br>
// ...<br>
void function() fptr = get_function_ptr();<br>
fptr.is_likely!likely_function();<br>
```<br>
See <br>
<a href="https://johanengelen.github.io/ldc/2016/04/13/PGO-in-LDC-virtual-calls.html" rel="noreferrer" target="_blank">https://johanengelen.github.io/ldc/2016/04/13/PGO-in-LDC-virtual-calls.html</a><br></blockquote><div><br></div><div>We virtually always end up with weird or butchered solutions to almost everything for no good reason. Why can't we just do the normal and reasonable thing for once?</div><div>Adding an attribute to a control statement is virtually zero impact; it's such an obvious and elegant solution. This also isn't mutually exclusive with expect(), or your suggestion above.<br></div></div></div>