TypeFunction example creatiing a conversion matrix

Adam D. Ruppe destructionator at gmail.com
Fri Oct 2 17:38:17 UTC 2020


On Friday, 2 October 2020 at 15:11:18 UTC, Adam D. Ruppe wrote:
> template pattern with 1,000 unique instances:
>
> 0.83s, 372,992 KB
>
> type function with 1,000 unique instances:

With bug fixed so it builds got:

0.45s, 85,000 KB


So there is a win, and there's a few reasons for it:

1) in the template the function is saved in RAM despite never 
being needed again. The typefunction impl is actually reused. 
This could potentially be optimized in the current code (I tried 
and failed though, kept segfaulting, but someone who knows dmd 
better might be able to pull it off)

2) The template function is larger. All those tuple foreaches are 
unrolled, leading to a large function. The typefunction does not 
do this.

I wonder if the type function's foreach implementation could be 
used in the tuple case too. At least if there's no variable 
declaration based on the iterated variable it seems plausible.

3) This also is kinda the other extreme. All reused args is the 
template's extreme case (one instance reused) and zero reused is 
the TF's extreme case (it runs each time because it legitimately 
must). I suspect most real code will land somewhere in the middle.


But still 2x as fast and 1/4 the memory with today's 
implementations is a real victory in this case.


More information about the Digitalmars-d mailing list