How templates work (1)

tsbockman thomas.bockman at gmail.com
Fri May 29 21:38:20 UTC 2020


On Friday, 29 May 2020 at 19:57:26 UTC, Stefan Koch wrote:
> Without memorization some things would be completely 
> impractical to do.

Yes, but does that mean that *everything* needs to be memoized? 
In other programming languages (and that's all template D really 
is, a programming language), memoization is used as needed, not 
*all the time*.

> Then again ignoring the implementation in dmd right now;
> There is no fundamental reason why anything has to be cached in 
> a purely functional system.
> since computing it again should yield the same values.
>
> As it stands the cache does only do good.
> It is not the slow part.
> Creating tons of ast nodes is however.

A single instantiation of std.meta.staticSort with a few hundred 
items requires well over 100 MB of memory which will not be freed 
until the compiler exits. It seems like it's not a problem only 
because it's impossible to write meta-programs that process 
non-trivial amounts of data, so people just don't.

(I would test with more items, but the compiler crashes due to 
hitting the template recursion limit - which is quite silly, 
given that the algorithm used, merge sort, only requires 
O(log(n)) non-tail levels in other programming languages.)

> For practical reasons dmd does not free intermediate results, 
> that would be the case
> even if the template instances were not cached.

DMD does try to free intermediate results with the "-lowmem" 
option:

     https://dlang.org/dmd-linux.html#switch-lowmem

Of course, the usefulness of this option is severely limited due 
to the forced memoization.


More information about the Digitalmars-d mailing list