Thread overview
Likelihood of if statement and case
5 days ago
Sergey
10 hours ago
Quirin Schroll
September 11

Given the recent thread by Manu, requesting a way to annotate likelihood for branches, I think I have found a way forward that is both in recognization of existing practices both in D and outside.

In this thread, Tejas provided a link that shows issues with the C/C++ design.

  1. The true path of a branch is assumed to be unlikely.
  2. The false path, or the fallthrough path if false path does not exist is assumed to be taken.
  3. D's if statement, is if-else, not if-elseif-else based, therefore it inherently has a priority and scope awareness and does not need a priority to be defined by the user.
  4. Switch statements have a priority for their cases by simply their order. The likely path is the default statement.

The unlikelihood is now defined against D using existing practices. No changes there.

What we want to define is a way to do a kind of swap, so to tell the compiler that no, the order in the code is the wrong way around.

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.

September 11
On 9/11/2024 2:26 AM, Richard (Rikki) Andrew Cattermole wrote:
> 4. Switch statements have a priority for their cases by simply their order. The likely path is the default statement.

The code is laid out in the order it appears in the source code, including the default.

5 days ago

On Wednesday, 11 September 2024 at 09:26:16 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

Given the recent thread by Manu, requesting a way to annotate likelihood for branches, I think I have found a way forward that is both in recognization of existing practices both in D and outside.

Just recent proposal for zig, that could be used as another example of design:

https://github.com/ziglang/zig/issues/21148

5 days ago
On 14/09/2024 7:49 AM, Sergey wrote:
> On Wednesday, 11 September 2024 at 09:26:16 UTC, Richard (Rikki) Andrew Cattermole wrote:
>> Given the recent [thread](https://forum.dlang.org/post/mailman.2451.1724377685.3719.digitalmars-d@puremagic.com) by Manu, requesting a way to annotate likelihood for branches, I think I have found a way forward that is both in recognization of existing practices both in D and outside.
> 
> Just recent proposal for zig, that could be used as another example of design:
> 
> https://github.com/ziglang/zig/issues/21148

So an attribute statement.

Nice idea, except we've got an example to show that this isn't such a great thing.

``pragma(inline)``, it can be used both as statement and as an attribute on a function.

Problem is the compiler may not see it when its a statement, depending upon how its been called during multi-step compilation.

It would be nice to convert it to a UDA, to clear this set of issues up.

And this tracks with the C/C++ attribute for likelihood and Tejas's article.

Good prior work content!

10 hours ago

On Friday, 13 September 2024 at 19:54:49 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

On 14/09/2024 7:49 AM, Sergey wrote:

>

On Wednesday, 11 September 2024 at 09:26:16 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

Given the recent thread by Manu, requesting a way to annotate likelihood for branches, I think I have found a way forward that is both in recognization of existing practices both in D and outside.

Just recent proposal for zig, that could be used as another example of design:

https://github.com/ziglang/zig/issues/21148

So an attribute statement.

Nice idea, except we've got an example to show that this isn't such a great thing.

pragma(inline), it can be used both as statement and as an attribute on a function.

Problem is the compiler may not see it when its a statement, depending upon how its been called during multi-step compilation.

It would be nice to convert it to a UDA, to clear this set of issues up.

And this tracks with the C/C++ attribute for likelihood and Tejas's article.

Good prior work content!

I can only repeat myself: An attribute is the wrong choice. Use a pragma. That’s what it’s for. Statement-level attributes require a grammar change. If you allow @whatever here specifically, one would assume any UDAs work there, too. But why would they? What would they mean? It’s really inconsistent.

10 hours ago
On 19/09/2024 2:46 AM, Quirin Schroll wrote:
> I can only repeat myself: An attribute is the wrong choice. Use a pragma. That’s what it’s for. Statement-level attributes require a grammar change. If you allow |@whatever| here specifically, one would assume any UDAs work there, too. But why would they? What would they mean? It’s really inconsistent.

They mean the same thing as they do anywhere else.

The fact that you don't have the ability to introspect them is irrelevant.

You can do the same thing for any symbol, or function parameter and you are free to never introspect them.

The only thing inconsistent is that there is no way to introspect it, but perhaps tooling could i.e. a documentation generator.