Library code:
class Resource
{
public:
typedef void (*func_sig)(int, char, double, void*);
//Registration
registerCallback(void* app_obj, func_sig func)
{
_app_obj = app_obj;
_func = func;
}
//Calling when the time comes
void call_app_code()
{
_func(231,'a',432.4234,app_obj);
}
//Other useful methods
private:
void* app_obj;
func_sig _func;
//Other members
};
Application Code:
class App
{
public:
void callme(int, char, double);
//other functions, members;
};
void callHelper(int i, char c, double d, void* app_obj)
{
static_cast<App*>(app_obj)->callme(i,c,d);
}
int main()
{
App a;
Resource r;
r.registercallback(&a, callHelper);
//Do something
}
The above is a minimal implementation of callback mechanism. It is more verbose, doesn't support binding, placeholders etc., like std::function.
If I use a std::function
or boost::function
for the above usecase, will there be any performance drawbacks? This callback is going to be in the very very critical path of a real time application. I heard that boost::function uses virtual functions to do the actual dispatch. Will that be optimized out if there are no binding/placeholders involved?
Update
For those interested in inspecting the assemblies in latest compilers: https://gcc.godbolt.org/z/-6mQvt
std::function
performs type erasure on the function type and there is more than one way to implement it, so you maybe should add which version of which compiler you are using to get an exact answer.boost::function
is largely identical to astd::function
and comes with an FAQ entry on call overhead and some general section on performance. Those give some hints on how a function object performs. If this applies in your case, depends on your implementation but numbers shouldn't be significantly different.I wondered myself quite frequently already, so I started writing some very minimal benchmark that attempts to simulate the performance by looped atomic counters for each function-pointer callback version.
Keep in mind, these are bare calls to functions that do only one thing, atomically incrementing its counter;
By checking the generated assembler output you may find out, that a bare C-function pointer loop is compiled into 3 CPU instructions;
a C++11's
std::function
call just adds 2 more CPU instructions, thus 5 in our example. As a conclusion: it absolutely doesn't matter what way of function pointer technique you use, the overhead differences are in any case very small.((Confusing however is that the assigned lambda expression seems to run faster than the others, even than the C-one.))
Compile the example with:
clang++ -o tests/perftest-fncb tests/perftest-fncb.cpp -std=c++11 -pthread -lpthread -lrt -O3 -march=native -mtune=native