[OT] my 10 minute talk about template-slowness

Michael Coulombe via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 21 11:34:13 PDT 2016


On Wednesday, 21 September 2016 at 17:14:34 UTC, Stefan Koch 
wrote:
> That can and is being fixed.
> Templates can only be fixed partially and I am not even sure of 
> that.
>
> I am not suggesting to remove templates.
> I just want to raise awareness that they have a rather high 
> cost.
> CTFE performance is fixable. Template performance might not.

Thinking about templates and CTFE, is part of the performance 
issue due to immutability? Compare std.meta.Reverse vs 
std.algorithm.reverse:

template Reverse(TList...)
{
     static if (TList.length <= 1)
         alias Reverse = TList;
     else
         alias Reverse = AliasSeq!(Reverse!(TList[$/2..$]), 
Reverse!(TList[0..$/2]));
}

void reverse(Range)(Range r) if (isRandomAccessRange!Range && 
hasLength!Range)
{
     immutable last = r.length-1;
     immutable steps = r.length/2;
     for (size_t i = 0; i < steps; i++)
     {
         r.swapAt(i, last-i);
     }
}

If we are able to get a bytecode CTFE interpretor, it seems like 
the same technology could be used to "run" templates like Reverse 
which only serves to compute a "value" rather than declare a new 
function or type, and perhaps be able to write a function like 
reverse which uses mutation and apply it to an AliasSeq in a 
typesafe way.


More information about the Digitalmars-d mailing list