3

I'm starting a new embedded project with C++ and I was wondering if it is too much expensive to use a interface oriented design. Something like this:

typedef int data;

class data_provider {

public:
    virtual data get_data() = 0;
};

class specific_data_provider : public data_provider {
public:
    data get_data() {
    return 7;
    }
};

class my_device {
public:
    data_provider * dp;
    data d;

    my_device (data_provider * adp) {
    dp = adp;
    d = 0;
    }

    void update() {
    d = dp->get_data();
    }
};

int
main() {
    specific_data_provider sdp;
    my_device dev(&sdp);

    dev.update();

    printf("d = %d\n", dev.d);

    return 0;
}
5
  • 3
    Too expensive compared to what? Aug 9, 2011 at 19:46
  • 1
    It's never too expensive, whatever you compare it to ;-) Ease of use, ease of maintenance - these first. Then bytes, cycles and others. Aug 9, 2011 at 19:47
  • Too expensive compared to writing the classes without interfaces, and making the calls direct to pointers of that class, and not the pointer to the interface.
    – ivarec
    Aug 9, 2011 at 19:53
  • 4
    @Haole: Sure. But how do you propose to emulate polymorphism? Either you don't need it (in which case it's not a fair comparison), or you do need it (in which case the C++ compiler will probably do a better job than you doing it manually). Aug 9, 2011 at 19:56
  • Ah, sorry, in my experience developers are often surprised by the memory costs associated with certain C++ features and that's what I answered.
    – Tod
    Aug 9, 2011 at 20:05

4 Answers 4

4

Inheritance, on its own, is free. For example, below, B and C are the same from a performance/memory point of view:

struct A { int x; };
struct B : A { int y; };
struct C { int x, y; };

Inheritance only incurs a cost when you have virtual functions.

struct A { virtual ~A(); };
struct B : A { ... };

Here, on virtually all implementations, both A and B will be one pointer size larger due to the virtual function.

Virtual functions also have other drawbacks (when compared with non-virtual functions)

  1. Virtual functions require that you look up the vtable when called. If that vtable is not in the cache then you will get an L2 miss, which can be incredibly expensive on embedded platforms (over 600 cycles on current gen game consoles for example).
  2. Even if you hit the L2 cache, if you branch to many different implementations then you will likely get a branch misprediction on most calls, causing a pipeline flush, which again costs many cycles.
  3. You also miss out on many optimisation opportunities due to virtual functions being essentially impossible to inline (except in rare cases). If the function you call is small then this could add a serious performance penalty compared to a inlined non-virtual function.
  4. Virtual calls can contribute to code bloat. Every virtual function call adds several bytes worth of instructions to lookup the vtable, and many bytes for the vtable itself.

If you use multiple inheritance then things get worse.

Often people will tell you "don't worry about performance until your profiler tells you to", but this is terrible advice if performance is at all important to you. If you don't worry about performance then what happens is that you end up with virtual functions everywhere, and when you run the profiler, there is no one hotspot that needs optimising -- the whole code base needs optimising.

My advice would be to design for performance if it is important to you. Design to avoid the need for virtual functions if at all possible. Design your data around the cache: prefer arrays to node-based data structures like std::list and std::map. Even if you have a container of a few thousand elements with frequent insertions into the middle, I would still go for an array on certain architectures. The several thousand cycles you lose copying data for the insertions may well be offset by the cache locality you will achieve on each traversal (Remember the cost of a single L2 cache miss? You can expect a lot of those when traversing a linked list)

1
  • 2
    He says (in a comment to another answer) he is using Cortex-M3; there is no cache. It does however have separate Flash and RAM buses and in most implementations a simple Flash accelerator to avoid instruction-fetch wait states. Clock rates are no greater than 120MHz, and RAM and Flash are typically on-chip, so there is seldom the speed mismatch between memory and processor that warrants a cache.
    – Clifford
    Aug 9, 2011 at 22:18
4

Inheritance is basically free. However, polymorphism and dynamic dispatch (virtual) have some consequences: each instance of a class with a virtual method contains a pointer to the vtable, which is used to select the right method to call. This adds two memory access for each virtual method call.

In most cases it won't be a problem, but it can become a bottleneck in some real time applications.

1
  • +1 right to the point. It's worth pointing out that, in some situations, it's possible to overcome the costs of calling a virtual method using the curiously recurring template pattern.
    – Vitor Py
    Aug 9, 2011 at 19:52
1

Really depends on your hardware. Inheritance per se probably doesn't cost you anything. Virtual methods cost you some amount of memory for the vTable in each class. Turning on exception handling probably costs you even more in both memory and performance. I have used all the features of C++ extensively on the NetBurner platform with chips like the MOD5272 which have a couple of Megs of Flash and 8 Megs of RAM. Also some things may be implementation dependent, on the GCC toolchain I use, when cout gets used instead of printf you take a big memory hit (it appears to link in a bunch of libraries). You might be interested in a blog post I wrote on the cost of type safe code. You would have to run similar tests on your environment to truly answer your question.

1
  • I'm under an ARM Cortex-M3 device. Hope it handles it. :)
    – ivarec
    Aug 9, 2011 at 19:54
0

The usual advice is to make the code clear and correct, and then think about optimisations only if it proves to be a problem (too slow or too much memory) in practice.

2
  • that is true but hardly what the OP was asking. Aug 9, 2011 at 19:51
  • That advice is like telling a bridge builder to only worry about the materials chosen once the bridge starts to crack. If you want performance then you need to design for it from the very beginning, otherwise you will end up having to make large, non-trivial changes to a lot of your code base. Aug 9, 2011 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.