TypeFunction example creatiing a conversion matrix
Stefan Koch
uplink.coder at googlemail.com
Fri Oct 2 03:35:50 UTC 2020
On Thursday, 1 October 2020 at 21:34:54 UTC, Walter Bright wrote:
> 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.
Linkers are not under our control.
The compiler is.
> 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.
So you are saying having a mechanism to replace a macro/template
with a regular function
does not address a language issue?
> 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 am intimately familiar with needsCodegen() (- and I still don't
fully understand it)
It leads to a stack overflow within the compiler when compiling
one our apps with -allinst.
Atila tries to fix it, and it has taken him a couple of weeks
already.
Right now the likely solution is to make it much more
conservative, because that's the only thing we can think of to
avoid linker errors.
> 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".
Been there. Done that.
> I suspect such a compiler improvement would produce
> considerable benefit to existing template-heavy code.
It requires the programmer to decide if a function needs to be
elided which in a template, he may not be able to tell.
Because it may depend on which instance is produced.
More information about the Digitalmars-d
mailing list