How make polymorphism optional?

This is a discussion on How make polymorphism optional? within the c++ forums in Programming Languages category; 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 { ...

Go Back   Application Development Forum > Programming Languages > c++

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-06-2008, 11:09 AM
Litvinov Sergey
Guest
 
Default How make polymorphism optional?

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?
Reply With Quote
  #2  
Old 09-06-2008, 01:09 PM
Erik Wikström
Guest
 
Default Re: How make polymorphism optional?

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
Reply With Quote
  #3  
Old 09-06-2008, 01:46 PM
Daniel T.
Guest
 
Default Re: How make polymorphism optional?

Litvinov Sergey <slitvinov@gmail.com> 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).


If speed is crucial, you should profile and find out where best to spend
your efforts, this is unlikely to be one of those places.

In any case:

vector<Base> vec(1); // make the vector as big as the
// number of objects you have
vec[0].method(); // no virtual overhead.
Reply With Quote
  #4  
Old 09-06-2008, 02:21 PM
Victor Bazarov
Guest
 
Default Re: How make polymorphism optional?

Daniel T. wrote:
> Litvinov Sergey <slitvinov@gmail.com> 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).

>
> If speed is crucial, you should profile and find out where best to
> spend your efforts, this is unlikely to be one of those places.
>
> In any case:
>
> vector<Base> vec(1); // make the vector as big as the
> // number of objects you have
> vec[0].method(); // no virtual overhead.


Are you sure about "no virtual overhead"? The vector:perator[]
returns a *reference*, not an object. With a reference a call to
a virtual function *should* go through the mechanism resolving all
the virtual function calls to the final overrider. It's not
statically resolved to Base::method, if that's what you implied.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Reply With Quote
  #5  
Old 09-06-2008, 03:52 PM
Litvinov Sergey
Guest
 
Default Re: How make polymorphism optional?

On Sep 6, 7:09 pm, Erik Wikström <Erik-wikst...@telia.com> 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/art...rticle2015.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.
Reply With Quote
  #6  
Old 09-06-2008, 03:59 PM
Litvinov Sergey
Guest
 
Default Re: How make polymorphism optional?

On Sep 6, 7:09 pm, Erik Wikström <Erik-wikst...@telia.com> 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.
Reply With Quote
  #7  
Old 09-06-2008, 04:42 PM
Ian Collins
Guest
 
Default Re: How make polymorphism optional?

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.
Reply With Quote
  #8  
Old 09-06-2008, 05:52 PM
Daniel T.
Guest
 
Default Re: How make polymorphism optional?

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote:
> Daniel T. wrote:
> > Litvinov Sergey <slitvinov@gmail.com> 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).

> >
> > If speed is crucial, you should profile and find out where best to
> > spend your efforts, this is unlikely to be one of those places.
> >
> > In any case:
> >
> > vector<Base> vec(1); // make the vector as big as the
> > // number of objects you have
> > vec[0].method(); // no virtual overhead.

>
> Are you sure about "no virtual overhead"? The vector:perator[]
> returns a *reference*, not an object. With a reference a call to
> a virtual function *should* go through the mechanism resolving all
> the virtual function calls to the final overrider. It's not
> statically resolved to Base::method, if that's what you implied.


Oops. I forgot about that.
Reply With Quote
  #9  
Old 09-06-2008, 10:35 PM
tony_in_da_uk@yahoo.co.uk
Guest
 
Default Re: How make polymorphism optional?

On Sep 7, 12:09*am, Litvinov Sergey <slitvi...@gmail.com> 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
Reply With Quote
  #10  
Old 09-08-2008, 11:10 PM
Jim Z. Shi
Guest
 
Default Re: How make polymorphism optional?



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). 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?


if you want `if' at compiling time, try template meta-programming.
it's safer and cooler than macro.

cheers,
Jim
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 06:39 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.