How make polymorphism optional?

Discussion in 'C++' started by Litvinov Sergey, Sep 6, 2008.

  1. My problem is the following one.

    I have a huge number of objects of base class:
    class Base {
    public:
    virtual void
    method();
    }

    And a derived class:
    class Derived : publcic Base {
    public:
    virtual void
    method();
    }

    Sometime I have no objects of Derived class and in those cases
    I would like to get rid of polymorphism overhead. (speed is crucial
    for
    me). It is OK for me to have
    a separate binary to handle those cases. But the only design I came up
    with is
    with preprocessor to "separate" virtual keyword in class definition

    class Base {
    #ifdefine NOPOLYMORPHISM
    void
    method();
    #else
    virtual void
    method();
    #endif
    }

    and the part of the program where the concrete type of the objects is
    defined should
    be also modified.

    Is there any better way to do that?
    Litvinov Sergey, Sep 6, 2008
    #1
    1. Advertising

  2. On 2008-09-06 17:09, Litvinov Sergey wrote:
    > My problem is the following one.
    >
    > I have a huge number of objects of base class:
    > class Base {
    > public:
    > virtual void
    > method();
    > }
    >
    > And a derived class:
    > class Derived : publcic Base {
    > public:
    > virtual void
    > method();
    > }
    >
    > Sometime I have no objects of Derived class and in those cases
    > I would like to get rid of polymorphism overhead. (speed is crucial
    > for
    > me).


    I have to ask, have you measured and made sure that it is the
    polymorphism that is your performance problem? If you have not carefully
    profiled your program yet you should do so before you consider how to
    speed it up.

    If you do not use the Derived class I think the easiest way would be to
    simply #ifdef out the declaration of it, the compiler should then be
    able to optimise away the polymorphism. Of course, there is also a
    chance that it already does so whenever it can.

    --
    Erik Wikström
    Erik Wikström, Sep 6, 2008
    #2
    1. Advertising

  3. On Sep 6, 7:09 pm, Erik Wikström <> wrote:
    > On 2008-09-06 17:09, Litvinov Sergey wrote:
    >
    >
    >
    > > My problem is the following one.

    >
    > > I have a huge number of objects of base class:
    > > class Base {
    > > public:
    > > virtual void
    > > method();
    > > }

    >
    > > And a derived class:
    > > class Derived : publcic Base {
    > > public:
    > > virtual void
    > > method();
    > > }

    >
    > > Sometime I have no objects of Derived class and in those cases
    > > I would like to get rid of polymorphism overhead. (speed is crucial
    > > for
    > > me).

    >
    > I have to ask, have you measured and made sure that it is the
    > polymorphism that is your performance problem? If you have not carefully
    > profiled your program yet you should do so before you consider how to
    > speed it up.
    >
    > If you do not use the Derived class I think the easiest way would be to
    > simply #ifdef out the declaration of it, the compiler should then be
    > able to optimise away the polymorphism. Of course, there is also a
    > chance that it already does so whenever it can.
    >


    Thank you for the comment. I have currently two versions and
    the profile (compiler: gcc 4.2.3, profiler: gprof) shows the
    difference
    worth the effort.

    Also I found that "compile time polymorphism"
    (http://www.gamedev.net/reference/articles/article2015.asp)
    does very similar thing but I cannot find how to adopt it to my case.
    In the article there are two different classes but in my case
    I have Class+Derived Class as one of the options.
    Litvinov Sergey, Sep 6, 2008
    #3
  4. On Sep 6, 7:09 pm, Erik Wikström <> wrote:
    > On 2008-09-06 17:09, Litvinov Sergey wrote:
    >
    >
    >
    > > My problem is the following one.

    >
    > > I have a huge number of objects of base class:
    > > class Base {
    > > public:
    > > virtual void
    > > method();
    > > }

    >
    > > And a derived class:
    > > class Derived : publcic Base {
    > > public:
    > > virtual void
    > > method();
    > > }

    >
    > > Sometime I have no objects of Derived class and in those cases
    > > I would like to get rid of polymorphism overhead. (speed is crucial
    > > for
    > > me).

    >
    > I have to ask, have you measured and made sure that it is the
    > polymorphism that is your performance problem? If you have not carefully
    > profiled your program yet you should do so before you consider how to
    > speed it up.
    >
    > If you do not use the Derived class I think the easiest way would be to
    > simply #ifdef out the declaration of it, the compiler should then be
    > able to optimise away the polymorphism. Of course, there is also a
    > chance that it already does so whenever it can.
    >
    > --
    > Erik Wikström



    Thank you for the comment. I have currently two versions and the
    profile (compiler: gcc 4.2.3, profiler: gprof) shows the difference
    worth the effort.

    Also I found that "compile time polymorphism" (http://www.gamedev.net/
    reference/articles/article2015.asp)
    does very similar thing but I cannot find how to adopt it to my case.
    The example in the
    articles deals with two different classes but I have Class+Derived
    Class as one
    of the options.
    Litvinov Sergey, Sep 6, 2008
    #4
  5. Litvinov Sergey

    Ian Collins Guest

    Litvinov Sergey wrote:
    > My problem is the following one.
    >
    > I have a huge number of objects of base class:
    > class Base {
    > public:
    > virtual void
    > method();
    > }
    >
    > And a derived class:
    > class Derived : publcic Base {
    > public:
    > virtual void
    > method();
    > }
    >
    > Sometime I have no objects of Derived class and in those cases
    > I would like to get rid of polymorphism overhead. (speed is crucial
    > for
    > me).


    Make Base a class template and specialise method().

    --
    Ian Collins.
    Ian Collins, Sep 6, 2008
    #5
  6. Litvinov Sergey

    Guest

    On Sep 7, 12:09 am, Litvinov Sergey <> wrote:
    > Sometime I have no objects of Derived class and in those cases
    > I would like to get rid of polymorphism overhead. (speed is crucial
    > for
    > me). It is OK for me to have
    > a separate binary to handle those cases. But the only design I came up
    > with is
    > with preprocessor to "separate" virtual keyword in class definition
    >
    > class Base {
    > #ifdefine NOPOLYMORPHISM
    >   void
    > method();
    > #else
    >   virtual void
    > method();
    > #endif
    >
    > }
    >
    > and the part of the program where the concrete type of the objects is
    > defined should
    > be also modified.
    >
    > Is there any better way to do that?


    Perhaps something like:

    struct Base
    {
    virtual void virtual_method() { base_method(); }
    void base_method();
    void method() { if (s_use_virtual_) virtual_method(); else
    base_method(); }
    static bool s_use_virtual_;
    };

    And set s_use_virtual_ at runtime based on whether you've created any
    derived objects. It still has some run-time overhead, but I think
    you'll find it's pretty small compared to out-of-line function
    invocation.

    HTH,

    Tony
    , Sep 7, 2008
    #6
  7. Litvinov Sergey

    Guest

    On Sep 7, 11:35 am, wrote:
    > On Sep 7, 12:09 am, Litvinov Sergey <> wrote:
    >
    >
    >
    > > Sometime I have no objects of Derived class and in those cases
    > > I would like to get rid of polymorphism overhead. (speed is crucial
    > > for
    > > me). It is OK for me to have
    > > a separate binary to handle those cases. But the only design I came up
    > > with is
    > > with preprocessor to "separate" virtual keyword in class definition

    >
    > > class Base {
    > > #ifdefine NOPOLYMORPHISM
    > > void
    > > method();
    > > #else
    > > virtual void
    > > method();
    > > #endif

    >
    > > }

    >
    > > and the part of the program where the concrete type of the objects is
    > > defined should
    > > be also modified.

    >
    > > Is there any better way to do that?

    >
    > Perhaps something like:
    >
    > struct Base
    > {
    > virtual void virtual_method() { base_method(); }
    > void base_method();
    > void method() { if (s_use_virtual_) virtual_method(); else
    > base_method(); }
    > static bool s_use_virtual_;
    >
    > };
    >
    > And set s_use_virtual_ at runtime based on whether you've created any
    > derived objects. It still has some run-time overhead, but I think
    > you'll find it's pretty small compared to out-of-line function
    > invocation.


    Actually, a faster option is to create a concrete class with the Base
    class's content, then a non-virtual inline function to access that
    content. You can put your code that operates on all the loaded data
    into a template function, and depending on whether you've seen a
    derived object call either the instantiation for the virtual-dispatch
    version or that for the concrete classes.

    Tony
    , Sep 9, 2008
    #7
  8. Litvinov Sergey

    Guest

    On Sep 9, 1:49 pm, wrote:
    > On Sep 7, 11:35 am, wrote:
    >
    >
    >
    > > On Sep 7, 12:09 am, Litvinov Sergey <> wrote:

    >
    > > > Sometime I have no objects of Derived class and in those cases
    > > > I would like to get rid of polymorphism overhead. (speed is crucial
    > > > for
    > > > me). It is OK for me to have
    > > > a separate binary to handle those cases. But the only design I came up
    > > > with is
    > > > with preprocessor to "separate" virtual keyword in class definition

    >
    > > > class Base {
    > > > #ifdefine NOPOLYMORPHISM
    > > >   void
    > > > method();
    > > > #else
    > > >   virtual void
    > > > method();
    > > > #endif

    >
    > > > }

    >
    > > > and the part of the program where the concrete type of the objects is
    > > > defined should
    > > > be also modified.

    >
    > > > Is there any better way to do that?

    >
    > > Perhaps something like:

    >
    > > struct Base
    > > {
    > >   virtual void virtual_method() { base_method(); }
    > >   void base_method();
    > >   void method() { if (s_use_virtual_) virtual_method(); else
    > > base_method(); }
    > >   static bool s_use_virtual_;

    >
    > > };

    >
    > > And set s_use_virtual_ at runtime based on whether you've created any
    > > derived objects.  It still has some run-time overhead, but I think
    > > you'll find it's pretty small compared to out-of-line function
    > > invocation.

    >
    > Actually, a faster option is to create a concrete class with the Base
    > class's content, then a non-virtual inline function to access that
    > content.  You can put your code that operates on all the loaded data
    > into a template function, and depending on whether you've seen a
    > derived object call either the instantiation for the virtual-dispatch
    > version or that for the concrete classes.
    >
    > Tony


    One more thing: even if you do have a derived object, you can
    explicitly force a call to the base class implementation using p-
    >Base::method();


    Tony
    , Sep 9, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Davenport via .NET 247

    Bind optional one-to-one relationship to textbox

    Dan Davenport via .NET 247, Aug 4, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    632
    Dan Davenport via .NET 247
    Aug 4, 2004
  2. Charles Shannon Hendrix

    REGEX: capturing on optional groups which fail

    Charles Shannon Hendrix, Jun 14, 2004, in forum: Perl
    Replies:
    0
    Views:
    549
    Charles Shannon Hendrix
    Jun 14, 2004
  3. Otto Wyss
    Replies:
    6
    Views:
    428
    Jeff Flinn
    Nov 9, 2004
  4. Krivenok Dmitry
    Replies:
    13
    Views:
    1,424
    Axter
    Jun 1, 2006
  5. K. Frank
    Replies:
    5
    Views:
    195
    James Kanze
    Aug 19, 2013
Loading...

Share This Page