[OT] my 10 minute talk about template-slowness

Stefan Koch via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 21 12:14:42 PDT 2016


On Wednesday, 21 September 2016 at 18:34:13 UTC, Michael Coulombe 
wrote:
> 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.

This would need to have special compiler support.
We would have to introduce another langauge feature that allows 
manipulating type-lists without going trough templates.
__traits give D insanely powerful introspection abilities by 
porviding the ability to do a query on types.
However the languages only way to work with types right now goes 
trough templates.
We would need something like type-functions that do not relay on 
template mechanisms.

I am not the right person to do language changes, but I am 
willing to implement a proposal should I hear of one that could 
solve this.


More information about the Digitalmars-d mailing list