TypeFunction example creatiing a conversion matrix

Walter Bright newshound2 at digitalmars.com
Thu Oct 1 21:34:54 UTC 2020


On 10/1/2020 1:57 AM, Stefan Koch wrote:
> However there is a difference in file size.
> 
> Type function version:
> 
> stat  makeConvMatrix.o
>    File: 'makeConvMatrix.o'
>    Size: 2876
> 
> VS template version
> 
> stat  makeConvMatrix_tmpl.o
>    File: 'makeConvMatrix_tmpl.o'
>    Size: 11760

Indeed there is, as the compiler generates code for the function and writes the 
code to the object file. The code it generates is placed into a COMDAT section 
which, since it is unreferenced, is supposed to be elided by the linker.

However, many linkers don't seem to do that, and besides, the compiler is 
wasting effort generating unused code. This is a more general inefficiency in 
the design of the compiler internals, and is not necessarily a language design 
defect.

Internally, there is a function called `needsCodegen()` which decides if a 
template instantiation needs code generated for it (duh!). It clearly errs on 
the side of being a bit too conservative.

I suggest an improvement to it where it will return false if the only use of a 
template instantiation is in CTFE. If that is unimplementable (the separate 
compilation model may make it impractical), a fallback position is to have a 
pragma or some other mechanism that says "don't generate code for this function".

I suspect such a compiler improvement would produce considerable benefit to 
existing template-heavy code.


More information about the Digitalmars-d mailing list