www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Virtual Function Optimization

reply Ben Phillips <Ben_member pathlink.com> writes:
How much does D optimize virtual function calls? Lets say you have the following
interface and class (not very useful ones).

interface Collection
{
public int size();
}

class Array : Collection
{
private int[] array;
public:
this() { array.length = 10; }
int size() { return array.length; }
}

Now consider the following statements

Collection c = new Array();
Array a = new Array();

c.size(); // clearly a virtual function call
a.size(); // ?????

Can the compiler deduce that "a.size()" should not be called through the vtable
since the type of "a" is not ambiguous?
Apr 30 2006
parent John Demme <me teqdruid.com> writes:
Ben Phillips wrote:

 How much does D optimize virtual function calls? Lets say you have the
 following interface and class (not very useful ones).
 
 interface Collection
 {
 public int size();
 }
 
 class Array : Collection
 {
 private int[] array;
 public:
 this() { array.length = 10; }
 int size() { return array.length; }
 }
 
 Now consider the following statements
 
 Collection c = new Array();
 Array a = new Array();
 
 c.size(); // clearly a virtual function call
 a.size(); // ?????
 
 Can the compiler deduce that "a.size()" should not be called through the
 vtable since the type of "a" is not ambiguous?
Probably not, but it's not too useful. In the slightly more generic case: Collection c = new Array(); Array a = ... c.size(); // clearly a virtual function call a.size(); // Also has to be a virtual function call, since a might be //a reference to a subclass I have heard Walter say (some time ago) that there are a good deal of optimizations be be done for OO code that he hasn't done yet. Based on my monitoring of the changelog, I doubt they've been done, but optimizations are not a priority in my book, anyway. ~John Demme
Apr 30 2006