Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How make polymorphism optional?

Reply
Thread Tools

How make polymorphism optional?

 
 
Litvinov Sergey
Guest
Posts: n/a
 
      09-06-2008
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
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      09-06-2008
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
 
 
 
 
Litvinov Sergey
Guest
Posts: n/a
 
      09-06-2008
On Sep 6, 7:09 pm, Erik Wikstrm <(E-Mail Removed)> 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
 
Litvinov Sergey
Guest
Posts: n/a
 
      09-06-2008
On Sep 6, 7:09 pm, Erik Wikstrm <(E-Mail Removed)> 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 Wikstrm



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
 
Ian Collins
Guest
Posts: n/a
 
      09-06-2008
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
 
tony_in_da_uk@yahoo.co.uk
Guest
Posts: n/a
 
      09-07-2008
On Sep 7, 12:09*am, Litvinov Sergey <(E-Mail Removed)> 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
 
tony_in_da_uk@yahoo.co.uk
Guest
Posts: n/a
 
      09-09-2008
On Sep 7, 11:35 am, (E-Mail Removed) wrote:
> On Sep 7, 12:09 am, Litvinov Sergey <(E-Mail Removed)> 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
 
Reply With Quote
 
tony_in_da_uk@yahoo.co.uk
Guest
Posts: n/a
 
      09-09-2008
On Sep 9, 1:49*pm, (E-Mail Removed) wrote:
> On Sep 7, 11:35 am, (E-Mail Removed) wrote:
>
>
>
> > On Sep 7, 12:09 am, Litvinov Sergey <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Dynamic polymorphism vs. Static polymorphism Krivenok Dmitry C++ 13 06-01-2006 09:49 AM
Polymorphism in GridView (ASP.Net 2.0) Kevin Lawrence ASP .Net 0 02-16-2006 11:32 AM
Help : Casting after a clone & method polymorphism Osaman Java 2 01-05-2004 03:59 PM
Polymorphism in xml ?? Sony Antony Java 0 08-26-2003 03:37 PM



Advertisments