Can the D compiler detect final and optimize?
Johan
j at j.nl
Sun Dec 27 00:25:54 UTC 2020
On Friday, 25 December 2020 at 19:29:26 UTC, Daniel Kozak wrote:
> On Fri, Mar 13, 2020 at 5:15 PM Dennis via Digitalmars-d <
> digitalmars-d at puremagic.com> wrote:
>
>> On Friday, 13 March 2020 at 15:08:34 UTC, IGotD- wrote:
>> > Instead of having final as default, can't the compiler
>> > itself infer when a class is implicitly final and do the
>> > optimizations itself?
>>
>> The problem is that only at runtime are all the derived
>> classes known, and by then the code is already compiled. I
>> think the Java Virtual Machine can automatically elide virtual
>> calls by virtue of being a JIT, but for D this would mean
>> either telling the compiler 'assume all derived classes are
>> present in the source files provided here' or doing fancy
>> link-time optimization.
>>
>
> I believe LTO could do that in some cases
"Whole program optimization", indeed. Search for `fwhole-program`
for GCC and Clang C++ compilers.
I don't know how it works for GDC (I think it may have an edge
over LDC on this topic), but for LDC we don't tell the optimizer
that our vtables are indeed vtables, so I'm not sure if
devirtualization works as well as for Clang (which does inform
the optimizer about vtables).
-Johan
More information about the Digitalmars-d
mailing list