TypeFunction example creatiing a conversion matrix

Adam D. Ruppe destructionator at gmail.com
Fri Oct 2 15:11:18 UTC 2020


On Friday, 2 October 2020 at 14:45:20 UTC, Stefan Koch wrote:
> now measure memory consumption during compilation :)

I did, it is there but small.

template:
14,096 KB

literal:
13,984 KB

your type function:
14,068 KB


In some individual runs there was enough variation where each one 
would come up on top and at bottom randomly but these are the 
average of 10 runs on my box.

Compile time too small to measure, all averaged 0.02s.

It is hard to say this would even matter if we did 1,000 of 
them.... let's try it.

added up top:

static foreach(i; 0 .. 1000)
mixin("struct i", i, " {}");


and in the function:

     static foreach(i; 0 .. 1000)
     mixin("static immutable convMatrix", i, " = 
makeConvMatrix(Byte, Ubyte, Short, Ushort, Int, Uint, Long, 
Ulong);");

     printf("%s\n", convMatrix0.ptr);



With type function:

0.37s, 71,756 KB

with template pattern:

0.06s, 34,944 KB

with string literal:

0.06s, 33,976 KB


The type function is a clear loser! But this is in part because 
the template is reusing its cached result. Let's break that by 
using those structs I generated

template pattern with 1,000 unique instances:

0.83s, 372,992 KB

type function with 1,000 unique instances:

  Error: function test.makeConvMatrix(alias[] types...) is not 
callable using argument types (i18, byte, ubyte, short, ushort, 
int, uint, long, ulong)
   cannot pass argument i18 of type i18 to parameter alias[] 
types...
[snip etc same thing for all 1000 cases]


Lovely. But I just grabbed your git branch so some in-progress 
work is to be expected to be incomplete...

What about a template?

test.d-mixin-42(42):        cannot pass argument test!18 of type 
test!18 to parameter alias[] types...


Is there a way I can pass it a list of unique types easily?


Probably fair to say it would perform about the same as before 
since the type function shouldn't be caching intermediate results 
(which is what makes the template fast before but bloat memory 
now), but would be nice to prove it.

PS for other readers, the template thing isn't *just* a cache, 
like it can't simply discard entries to it.... yet. With this 
specific pattern, the compiler actually *should* be able to free 
it  the helper function and even discard the whole cache in 
theory, but in practice that can only be done in specialized 
cases and it is very difficult to realize.


More information about the Digitalmars-d mailing list