Are these bencmarks recent and real?

Ali Çehreli acehreli at yahoo.com
Tue Aug 31 15:44:42 UTC 2021


On 8/31/21 8:09 AM, Steven Schveighoffer wrote:

 > `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.

[I change my understanding at the end of this post.]

But 'const' is not 'static const'. pragma(msg) is being extra helpful by 
hoping for the availability of the value. (To me, 'const' means "I 
promise I will not mutate", which has no relation to compile-time 
availability.)

 > 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.

It makes sense but there are two minor disturbances:

1) This is an example of "It went better than I expected".

2) Success is determined by trying.

The following program fails compilation when it gets to 'args.length' 
after 10 second of compilation:

import std.range;
import std.algorithm;

void main(string[] args) {
   const ret = 4_000_000.iota.sum + args.length;
   pragma(msg, ret);
}

Well, nothing is *wrong* here but concepts are muddled. But then I even 
more TIL that this is the same for templates:

void foo(int i)() {
}

void main() {
   const i = 42;
   foo!i();    // Compiles
}

You know... Now it feels I knew it all along because CTFE is about 
expressions, not variables. I need not have a "compile-time variable". 
Makes sense now. :)

Ali



More information about the Digitalmars-d mailing list