How templates work (1)

tsbockman thomas.bockman at gmail.com
Fri May 29 22:31:54 UTC 2020


On Friday, 29 May 2020 at 21:53:33 UTC, Stefan Koch wrote:
> I haven't seen staticSort so far.
> I shall have a look at it.

Your type function project, newCTFE, and the ability for the 
compiler to discard intermediate results in CTFE would, combined, 
enable meta algorithms that scale reasonably, I think. The 
ability to mark templates (or maybe specific instantiations?) 
@noMemo to disable memoization would be another approach.

Anyway, here's some test code I wrote:

import std.meta, std.random, std.stdio;

immutable(int[]) randomItems(int n) pure @trusted nothrow {
     assert(__ctfe);

     auto rand = Xorshift(n);
     int[] ret = new int[n];
     for(size_t x = 0; x < n; ++x) {
         ret[x] = rand.front;
         rand.popFront();
     }

     return cast(immutable) ret;
}

struct S(int _x) { enum int x = _x; }
enum int X(S) = S.x;
template less(A, B) { enum bool less = A.x < B.x; }
alias boom = staticSort!(less, staticMap!(S, 
aliasSeqOf!(randomItems(384))));

void main()
{
     writeln([ staticMap!(X, boom) ]);
}

> The weka fork of ldc has a dynamically configure-able recursion 
> limit.
> And dmd can be trivially patched to take it as a runtime option 
> as well.
> Just in case you do want to see what happens if you hit the 
> _real_ limit ;)

I have actually hit the real limit by accident before; that's how 
I discovered that it's possible to crash my operating system just 
by burning too much memory in user space. It's also why I now 
have 32 GB of RAM on my workstation. :-)

For anyone else having trouble with out-of-memory related crashes 
on Linux, I recommend replacing dmd in your PATH with a script 
like this to prevent the OS from crashing:

     (using ulimit -v $MAX_MEMORY_IN_KB; /path/to/dmd $@)



More information about the Digitalmars-d mailing list