Are these bencmarks recent and real?

Steven Schveighoffer schveiguy at gmail.com
Tue Aug 31 15:09:11 UTC 2021


On 8/30/21 1:15 PM, Ali Çehreli wrote:
> On 8/30/21 8:46 AM, rikki cattermole wrote:
>>
>> On 31/08/2021 3:34 AM, jfondren wrote:
>>> That D can compile amazingly fast can be shown just by building dmd 
>>> itself.
>>
>> "Oh let me recompile dmd" - Stefan
>>
>> A little gag from this BeerConf and yeah, it builds fast.
> 
> The following program takes 10 seconds on my computer. How is that fast? :p
> 
> import std.range;
> import std.algorithm;
> 
> int main() {
>    enum ret = 4_000_000.iota.sum;
>    // pragma(msg, ret);
>    return ret ^ ret;
> }
> 
> (Of course I am joking: Replacing 'enum' with e.g. 'const' makes it fast.)
> 
> However, TIL: pragma(msg) works with 'const' variables! (At least with 
> that one.) Replace 'enum' with 'const' and pragma(msg) computes it at 
> compile time. But... but... 'const' doesn't really mean compile-time... 
> Is that intended? There is some semantic confusion there. :/

initializers *sometimes* can be computed at compile time. If assigned to 
a const or immutable variable, the compiler is smart enough to know that 
the item hasn't changed, and so it can go back to the static initializer 
for what the value actually is.

what is happening here:

`enum ret = 4_000_000.iota.sum;`

In this case, you are requesting a compile time constant, and so it runs 
CTFE here to generate the result.

`const ret = 4_000_000.iota.sum;`

In this case, since this is inside a function, and not assiged to a 
global or static variable, it is generated at runtime.

`pragma(msg, ret);`

However, here we are requesting the value of `ret` at compile time. The 
compiler knows that since it's const, it should have the value it's 
initialized with. So it runs the *initializer* expression 
`4_000_000.iota.sum` at compile-time, and now it has access to the 
value. So actually, the CTFE engine runs here instead of at `ret`'s 
initialization.

If a const variable depended on an expression that could only be 
computed at runtime (like say with the value of an input parameter), 
then the `pragma(msg)` would NOT work.

-Steve


More information about the Digitalmars-d mailing list