How make polymorphism optional?

  • Thread starter Litvinov Sergey
  • Start date
L

Litvinov Sergey

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?
 
E

Erik Wikström

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.
 
L

Litvinov Sergey

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.
 
L

Litvinov Sergey

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.
The example in the
articles deals with two different classes but I have Class+Derived
Class as one
of the options.
 
I

Ian Collins

Litvinov said:
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().
 
T

tony_in_da_uk

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
 
T

tony_in_da_uk

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
 
T

tony_in_da_uk

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top