PeddleSpam
PeddleSpam

Reputation: 438

Obtain pointer to virtual method?

I have a function that needs to call a virtual method many times in a loop and was hoping there would be a way to avoid the overhead of a vtable lookup every time. I thought maybe obtaining a pointer to the method would be a good way round this. The code below shows what I'm trying to do, the problem being that the address of a method in Derived cannot be assigned to a Base member function pointer.

class Base
{
    public:

    typedef float ( Base::*MFP )( float const & x ) const;

    virtual MFP getMFP( void ) const = 0;

    virtual float function( float const & x ) const = 0;
};

class Derived : public Base
{
    public:

    virtual MFP getMFP( void ) const
    {
        return &Derived::function;
    }

    virtual float function( float const & x ) const
    {
        return x * x;
    }
};

class Other
{
    public:

    float calculate( float const & x, Base * pBase ) const
    {
        Base::MFP function = pBase->getMFP();
        return ( ( *pBase ).*( function ) )( x );
    }
};

Is there a way to do what I'm trying to do here?


EDIT:

For anyone who's still interested I ran a timed test of my code. Turns out dynamic dispatch only slowed down my calculation method by 0.004%, so pretty much nothing. Compiled using MSVC 2010 with full optimisation.

Upvotes: 2

Views: 228

Answers (4)

AnT stands with Russia
AnT stands with Russia

Reputation: 320481

Your idea is based on an incorrect assumption that creating such pointer will take VMT access out of the loop (i.e. perform it once instead of performing it repeatedly in the loop).

In C++ language calls through pointers of "pointer-to-member function" type (which happen to be bound to a virtual member function) are always resolved at the moment of the call, not at the moment of initialization. This means that even if you create such a pointer, it will not optimize anything at all. Each call in the loop will still perform a full-blown access to VMT, just like it was without any pointers.

In C++ there's no way to force such a pointer to point to a specific version of a virtual function at the moment of initialization. C++ language simply has no such feature.

Upvotes: 4

Lucretiel
Lucretiel

Reputation: 3324

The easiest way would be a cast, if you know for certain that the object in the for loop is a Derived.

void foo(float);
Base* pObject;
//...
//If you know with certainty that the object is a Derived
Derived& der = *static_cast<Derived*>(object)
for(float x : floatContainer)
{
     foo(der.function(x));
}

This means you'll only invoke Derived's vtable, which is potentially faster depending on how many classes derive from Derived. If all you can guarantee is that the class is a Base, then what you've written in the question above is essentially a vtable, except much slower than anything the compiler would produce. You may have heard that vtables are slow, and maybe that's true compared to raw C pointers and functions, but I can all but completely guarantee that it'll be fast enough for what you need.

Upvotes: 0

Rafael Baptista
Rafael Baptista

Reputation: 11499

Don't do it. The processor has deep pipelines, and the compiler is almost certainly already caching the function pointer. You can do this with some effort. But I guarantee that on any compiler less than 10 years old this will make no difference.

Upvotes: 2

keelerjr12
keelerjr12

Reputation: 1903

I would avoid premature optimization until it becomes a problem; and then, and only then, after you have profiled your code do you make changes. Shoot for maintainability and extendability first, and if you still are having performance issues look to refactor. In this example, the look-ups have very little overhead.

I'd rather look at code that is readable than code that has a 5% performance increase but takes me all day to figure out what it's doing.

Upvotes: 0

Related Questions