www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - inlining final functions

reply n00b <n00b nospam.com> writes:
Is the following correct :

class X
{
	void foo() { ... }
}
class Y : X
{
	final override void foo() { ... }
}
	

Y y = new Y;
y.foo; //inlined
X x = y;
x.foo; //not inlined
Feb 26 2013
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Tuesday, February 26, 2013 10:20:21 n00b wrote:
 Is the following correct :
 
 class X
 {
 	void foo() { ... }
 }
 class Y : X
 {
 	final override void foo() { ... }
 }
 
 
 Y y = new Y;
 y.foo; //inlined
 X x = y;
 x.foo; //not inlined
Y.foo can be made non-virtual by the compiler, because it's final. X.foo can't be, because it's overridden, and virtual functions can't normally be inlined, because they have to be looked up at runtime (because they depend on the underlying type, not the type of the reference). A particularly advanced optimizer might be able to inline them both in this case, because it's possible to see that x is really a Y by following the flow of the code, but dmd doesn't generally do flow analysis, so it won't figure that out, and as soon as the code isn't this straightforward, even a more advanced optimizer couldn't figure that out. On top of that, dmd's inliner is generally accused of being fairly poor (though I don't know how good it really is for certain, because I've never sat down to try and figure out for sure what is and isn't inlined). So, yes. What you're seeing is perfectly correct and expected. - Jonathan M Davis
Feb 26 2013