druschel@cs.arizona.edu (Peter Druschel) (08/25/90)
Consider the following code (compiled with g++ 1.37.1): class Base { public: inline virtual void foo() {}; }; class Derived : public Base { public: inline virtual void foo() {}; }; class Container { public: inline void bar(Base &b) {b.foo();}; }; main() { Derived d; Container c; d.foo(); /* this call to Derived::foo() is statically resolved and inline expanded */ c.bar(d); /* this call to Container::bar() is also statically resolved and inline expanded the virtual function foo(), called within bar(), however, is invoked indirectly through Derived's virtual table. */ } Note that bar() is inline expanded in main() and the argument d passed to bar() is known to be of class Derived. Thus, the exact type of the argument passed to foo() from within bar() is also known. Consequently, foo() could also be statically resolved and inline expanded. It seems to me that with little effort, the compiler could notice this situation and take advantage of it. This optimization would be very useful; in many cases, it could even eliminate the need for parametric container classes. Usually, the member functions of container classes invoke small virtual functions of their arguments (e.g. the contained objects). In many cases, the member functions (of the container class) themselves are short and can be inlined. If the compiler was capable of doing the above optimization, it could eliminate all the virtual invocations. The resulting container class would be just as efficient as a parametric container class created specifically for a particular (contained) object class. What do people think about this? Is there any chance this (and other strategies for exploring static resolution of virtual functions, as hinted in the "ideas" file in the distribution) will be implemented any time soon? I have thought about making the required modifications to g++, but I don't know enough about g++ internals to be able to estimate what would be involved. Some information about how g++ does inlining and a few hints might encourage me to do it. -- Peter Druschel Internet: druschel@cs.arizona.edu Dept of Computer Science University of Arizona Tucson, AZ 85721 USA