Drew Frank
Drew Frank

Reputation: 590

Inline virtual function when called from another virtual function?

I have a class with two virtual member functions: foo and wrapper. foo is short and fast, and wrapper contains a loop that calls foo many times. My hope is that there is some way to inline the calls to foo inside the wrapper function, even when called from a pointer to an object:

MyClass *obj = getObject();
obj->foo(); // As I understand it, this cannot be inlined. That's okay.
obj->wrapper(); // Nor will this. However, I hope that the machine code
                // for the wrapper function will contain inlined calls to
                // foo().

Essentially, I want the compiler to generate multiple versions of the wrapper function -- one for each possible class -- and inline calls to the appropriate foo, which should be possible since the object type is determined before picking which wrapper function to execute. Is this possible? Do any compilers support this optimization?

Edit: I appreciate all of the feedback and answers so far, and I may end up picking one of them. However, most responses ignore the last part of my question where I explain why I think this optimization should be feasible. That is really the crux of my question and I am still hoping someone can address that.

Edit 2: I picked Vlad's answer since he both suggested the popular workaround and partially addressed my proposed optimization (in the comments of David's answer). Thanks to everyone who wrote an answer -- I read them all and there wasn't a clear "winner".

Also, I found an academic paper that proposes an optimization very similar to what I was suggesting: http://www.ebb.org/bkuhn/articles/cpp-opt.pdf.

Upvotes: 3

Views: 444

Answers (5)

Mark Ransom
Mark Ransom

Reputation: 308148

Imagine the following hierarchy:

class Base
{
    virtual void foo();
    virtual void wrapper();
};

class Derived1: public Base
{
    virtual void foo() { cout << "Derived1::foo"; }
    virtual void wrapper() { foo(); }
};

class Derived2: public Derived1
{
    virtual void foo() { cout << "Derived2::foo"; }
};

Base * p1 = new Derived1;
p1->wrapper();  // calls Derived1::wrapper which calls Derived1::foo
Base * p2 = new Derived2;
p2->wrapper();  // calls Derived1::wrapper which calls Derived2::foo

Can you see the problem? Derived1::wrapper must call Derived2::foo. It can't know until runtime whether it will be calling Derived1::foo or Derived2::foo, so there's no way to inline it.

If you want to insure that inlining is possible, make sure that the function you want to inline isn't virtual. It seems from your description that this might be possible, if every class in the hierarchy reimplements both foo and wrapper. A function doesn't need to be virtual to be overridden.

Upvotes: 2

Luchian Grigore
Luchian Grigore

Reputation: 258588

This is not accurate. virtual functions can be inlined, but only if the compiler knows the static type of the object with certainty - and thus have the guarantee that polymorphism works.

For example:

struct A
{
    virtual void foo()
    {
    }
};
struct B
{
    virtual void foo()
    {
    }
};

int main()
{
    A a;
    a.foo(); //this can be inlined
    A* pa = new A;
    pa->foo(); //so can this
}

void goo(A* pa)
{
    pa->foo() //this probably can't
}

That said, it appears that in your case this can't happen. What you can do is have another non-virtual function that actually implements the functionality and call it statically, so the call gets resolved at compile-time:

class MyClass
{
    virtual void foo() = 0;
    virtual void wrapper() = 0;
};

class Derived : MyClass
{
    void fooImpl()
    {
       //keep the actual implementation here
    }
    virtual void foo()
    {
       fooImpl();
    }
    virtual void wrapper()
    {
       for ( int i = 0 ; i < manyTimes ; i++ )
          fooImpl(); //this can get inlined
    }
};

or simply Derived::foo() as pointed out by @avakar.

Upvotes: 1

No, the function call will not be inlined if performed through a pointer or reference (this includes the this pointer). A new type can be created that extends from your current type and overrides foo without overriding wrapper.

If you want to enable the compiler to inline your function, you must disable virtual dispatch for that call:

void Type::wrapper() {
    Type::foo();            // no dynamic dispatch
}

Upvotes: 2

user405725
user405725

Reputation:

In certain cases, compiler can determine the virtual dispatch behavior in compile-time and perform non-virtual function invocation or even inline the function. It can only do that if it can figure out that your class is the "top" in inheritance chain or those two functions are not otherwise overloaded. Oftentimes, this is simply impossible, especially if you don't have late time optimization enabled for the whole program.

Unless you want to check the results of your compiler's optimizations, your best bet would be not to use a virtual function in the inner loop at all. For example, something like this:

class Foo {
  public:
    virtual void foo()
    {
        foo_impl();
    }

    virtual void bar()
    {
        for (int i = 0; i < ∞; ++i) {
            foo_impl();
        }
    }

  private:
    void foo_impl() { /* do some nasty stuff here */ }
};

But in that case you clearly give up the idea that somebody may come in, inherit from your class and throw in their own implementation of "foo" to be called by your "bar". They will essentially will need to re-implement both.

On the other hand, it smells a bit like a premature optimization. Modern CPUs will most likely "lock" your loop, predict the exit from it and execute the same µOPs over and over, even if your method is virtually virtual. So I'd recommend you carefully determine this to be a bottleneck before spending your time optimizing it.

Upvotes: 3

BigBoss
BigBoss

Reputation: 6914

Your function is virtual and its type is not indicate until runtime, so how you expect compiler to inline code of some class into it?

In some similar situation I had 2 functions: foo that is virtual and foo_impl that is normal function and will be called from foo and wrapper

Upvotes: 0

Related Questions