Sign in to follow this  

Inheritance overhead

This topic is 4664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Exists some overhead of using inheritance against make-all-classes-from-scratch ? although it will be minimum (1 or 2 instructions more). For example in this pseudo-C++ example:
class A {
private: data
public: general_functions
};

class B : public A {
private: more_data_B
public: specific_B_functions
};

class C : public B {
private: more_data_C
public: specific_C_functions
};

// vs

class A {
private: data
public: general_functions
};

class B {
private: data
private: mode_data_B
public: general_functions
public: specific_B_functions
};

class C {
private: data
private: mode_data_B
private: mode_data_C
public: general_functions
public: specific_B_functions
public: specific_C_functions
};

I am talking about overhead, simple curiosity, I know that this simple thing doesn't affect final speed of a program, and I know that not using inheritance is more typying work. Thanks

Share this post


Link to post
Share on other sites
Using inheritance itself doesn't cost anything, its when you introduce a least one virtual member function in to picture you gain often neglibable overhead, for more details on exactly what you pay for read this

Share this post


Link to post
Share on other sites
Quote:
Original post by Silly_con
in a simple example like the posted, isn't needed to make a virtual destructor ?


If you intend on invoking operator delete on base pointers that refers to derived instances you should make the destructor virtual to avoid undefined behaviour.

Share this post


Link to post
Share on other sites
There is the potential for a minimal amount of wasted memory space. Let's assume for a moment the data section for each class is one int and one char.

If you choose to reimplement everything every time, you can write class C as such:

class C
{
private:
int A_int;
int B_int;
int C_int;
char A_char;
char B_char;
char C_char;
public:
//...
};


Now, the above done via inheritence would look something more like this:

class C : public B
{
private:
int C_int;
char C_char;
public:
//...
};


Which looks something like this in memory:

class C
{
private:
int A_int;
char A_char;
char padding_so_B_int_starts_on_a_word_boundry_for_performance_reasons[3];
int B_int;
char B_char;
char padding_so_C_int_starts_on_a_word_boundry_for_performance_reasons[3];
int C_int;
char C_char;
public:
//...
};


That said, going for the first method would probably result in WORSE performance, for this reason: The function(s) that operate on C would need to be recompiled. A::non_overridden_function() and C::non_overriden_function() would be different, and would require extra memory. That in and of itself is not the problem - the problem is that it's going to be more likely to result in page thrashing.

The first method is also more obviously much harder to mantain.

I've never found a time that I felt the need to use the first version. The only time it should be used is if you've PROFILED FIRST with a GOOD profiler (I'm talking the kind that analizes things like cache consistancy) and you've determined for certain that your major bottleneck is memory bandwidth when iterating over a large series that consists of only objects of class C.

Share this post


Link to post
Share on other sites

This topic is 4664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this