No CTFE of function

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 27 20:16:24 PDT 2017


On Sunday, 27 August 2017 at 17:47:54 UTC, Cecil Ward wrote:
>
> I wonder if there is anything written up anywhere about what 
> kinds of things are blockers to either CTFE or to successful 
> constant-folding optimisation in particular compilers or in 
> general?
>
> Would be useful to know what to stay away from if you really 
> need to make sure that horrendously slow code does not get run 
> at runtime. Sometimes it is possible or even relatively easy to 
> reorganise things and do without certain practices in order to 
> win such a massive reward.

The rules for CTFE are outlined in the docs [1]. What is 
described there is all there is to it. If those criteria are not 
met, the function cannot be executed at compile time. More 
importantly, as mentioned earlier in the thread, CTFE will only 
occur if a function *must* be executed at compile time, i.e. it 
is in a context where the result of the function is required at 
compile-time. An enum declaration is such a situation, a variable 
initialization is not.

There are also a couple of posts on the D Blog. Stefan has 
written about the new CTFE engine [1] and I posted something 
showing a compile-time sort. These illustrate the points laid out 
in the documentation.

As for compiler optimizations, there are some basic optimizations 
that will be common across all compilers, and you can google for 
compiler optimizations to find such generalities. Many of these 
apply across languages, and those specific to the C-family 
languages will likely be found in D compilers. Beyond that, I'm 
unaware of any documentation that outlines optimizations in D 
compilers.

[1] https://dlang.org/spec/function.html#interpretation
[2] https://dlang.org/blog/2017/04/10/the-new-ctfe-engine/
[3] https://dlang.org/blog/2017/06/05/compile-time-sort-in-d/


More information about the Digitalmars-d-learn mailing list