How templates work (1)

Stefan Koch uplink.coder at googlemail.com
Fri May 29 21:53:33 UTC 2020


On Friday, 29 May 2020 at 21:38:20 UTC, tsbockman wrote:
> On Friday, 29 May 2020 at 19:57:26 UTC, Stefan Koch wrote:
>> [...]
>
> 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*.
>
>> [...]
>
> 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.)
>
>> [...]
>
> 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.

I haven't seen staticSort so far.
I shall have a look at it.

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 ;)


More information about the Digitalmars-d mailing list