Final necessary for inline optimization?
IGotD-
nise at nise.com
Sat Oct 19 15:58:08 UTC 2019
When I'm playing around with different D compilers I get
different results regarding class method inlining. According most
information I have seen, you must declare a class method final
for the compiler to be able inline the method otherwise it will
be a virtual table call.
Take this simple example.
import std.stdio;
import std.random;
class TestClass
{
static auto rnd = Random(42);
int tt = 3;
void doStuff()
{
tt = uniform(0, 15, rnd);
}
}
void main()
{
auto c = new TestClass;
c.doStuff();
writeln("TestClass is ", c.tt);
}
The random generator is used here so that CTFE doesn't kick in.
DMD certainly doesn't inline unless I use final (on class or
doStuff) but when I use LDC, LDC seems to be able to figure out
that the class isn't derived and inlines it regardless (-O option
required). I see that LDC generates the method doStuff so it
could also be that LDC knows which specific class it is (and not
a derived one) in main and can inline for this particular case.
Which one is it, LDC recognizes TestClass isn't derived or is
sure that the class (c) isn't derived in particular? Would that
mean that the vtable entry for doStuff is created regardless? Any
other information why LDC do better?
I have previously seen a failed attempt to make final default
(https://forum.dlang.org/thread/lfqoan$5qq$1@digitalmars.com?page=1) but would that be necessary. Could the compiler figure out that a class method is final and manage that optimization by itself?
More information about the Digitalmars-d-learn
mailing list