I have a C++ application that can be simplified to something like this:
class AbstractWidget {
public:
virtual ~AbstractWidget() {}
virtual void foo() {}
virtual void bar() {}
// (other virtual methods)
};
class WidgetCollection {
private:
vector<AbstractWidget*> widgets;
public:
void addWidget(AbstractWidget* widget) {
widgets.push_back(widget);
}
void fooAll() {
for (unsigned int i = 0; i < widgets.size(); i++) {
widgets[i]->foo();
}
}
void barAll() {
for (unsigned int i = 0; i < widgets.size(); i++) {
widgets[i]->bar();
}
}
// (other *All() methods)
};
My application is performance-critical. There are typically thousands of widgets in the collection. The classes derived from AbstractWidget (of which there are dozens) typically leave many of the virtual functions not overridden. The ones that are overridden typically have very fast implementations.
Given this, I feel I can optimize my system with some clever meta-programming. The goal is to leverage function inlining and to avoid virtual function calls, while keeping the code managable. I’ve looked into the Curiously Recurring Template Pattern (see here for description). This seems to almost do what I want, but not quite.
Is there any way to make the CRTP work for me here? Or, is there any other clever solution anyone can think of?
CRTP or compile-time polymorphism is for when you know all of your types at compile time. As long as you’re using
addWidgetto collect a list of widgets at runtime and as long asfooAllandbarAllthen have to handle members of that homogenous list of widgets at runtime, you have to be able to handle different types at runtime. So for the problem you’ve presented, I think you’re stuck using runtime polymorphism.A standard answer, of course, is to verify that the performance of runtime polymorphism is a problem before you try to avoid it…
If you really need to avoid runtime polymorpism, then one of the following solutions may work.
Option 1: Use a compile-time collection of widgets
If your WidgetCollection’s members are known at compile time, then you can very easily use templates.
Option 2: Replace runtime polymorphism with free functions
Ugly, and really not OO. Templates could help with this by reducing the need to list every special case; try something like the following (completely untested), but you’re back to no inlining in this case.
Option 3: Eliminate OO
OO is useful because it helps manage complexity and because it helps maintain stability in the face of change. For the circumstances you seem to be describing – thousands of widgets, whose behavior generally doesn’t change, and whose member methods are very simple – you may not have much complexity or change to manage. If that’s the case, then you may not need OO.
This solution is the same as runtime polymorphism, except that it requires that you maintain a static list of “virtual” methods and known subclasses (which is not OO) and it lets you replace virtual function calls with a jump table to inlined functions.