Standard way to supply hints to branches
Manu
turkeyman at gmail.com
Sat Aug 24 16:47:38 UTC 2024
On Sat, 24 Aug 2024 at 21:46, IchorDev via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:
> On Saturday, 24 August 2024 at 09:11:34 UTC, user1234 wrote:
> > On Saturday, 24 August 2024 at 08:06:21 UTC, IchorDev wrote:
> >> I think everyone in this thread can agree that we need a
> >> compiler-agnostic solution for changing the presumed hot-path,
> >> and one that doesn’t require rewriting existing code more than
> >> just adding on one word. For example, `if((x ==
> >> y).expect(false)){}` makes it a pain to rewrite existing
> >> `if`s, where `@unlikely if(x == y) {}` is easy.
> >
> > You also have the `pragma` option. I think that would be more
> > adequate as not every vendor has to support it. In other words
> > "dmd can just ignore it". Bonus: pragma can be attached to
> > statements, that's not the case of attributes.
>
> Like `pragma(likely, false)`? It’s a bit long, maybe
> `pragma(unlikely)`? It’s something I’d want to use fairly often,
> after all. Or we could just add some new grammar for adding
> special modifiers to if statements. Something like…
> ```d
> if: unlikely(x){
> //…
> }
> if(!x){
> //…
> }else: likely{
> //…
> }
> ```
>
C++ is way ahead on this... they added attributes to the language years
back, and they defined [[likely]] and [[unlikely]] post-fix on control
statements. It's really convenient, it reads well, and it's easy to
retrofit without disturbing the code.
That's the de-facto now, and we should follow it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20240825/5261b331/attachment.htm>
More information about the Digitalmars-d
mailing list