Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C ++ Boost Bind Performance

Are there any performance impacts (positive or negative) when binding functions (using Boost Bind) ?

like image 731
tropicana Avatar asked Sep 16 '12 11:09

tropicana


2 Answers

Maybe, may not be. It depends.

The result of std::bind (or also boost::bind) is a so-called "bind expression", which has an un­know­able type determined by the implementation. This type is a Callable, and it is convertible to an in­stance of std::function (or boost::function).

Internally, function (may) use type erasure to handle various complex, stateful "call­able objects". This entails a dynamic allocation and a virtual dispatch in some (though not neces­sari­ly all) cases. Both bind and function are stateful, since they store the bound arguments.

The upshot is that you should avoid converting a bind expression to a function object if possible. The bind expression itself may be cheaper, and you should not be afraid of using bind (for example when bind­ing member function pointers to instances and arguments). Use bind freely, but conversion to function only if you truly need to manage a heterogeneous collection of callable entities.

Here are two typical examples:

Bad; avoid this:

std::function<int(bool, char)> f = std::bind(&Foo::bar, x, 12);

void do_something(std::function<int()> func, int & acc)
{
    acc += func();
}

Better; prefer this:

auto f = std::bind(&Foo::bar, x, 12);   // unknowable type, but perfectly fine

template <typename F>
void do_something(F && func, int & acc)  // can deduce unknowable types
{
    acc += func();
}
like image 153
Kerrek SB Avatar answered Sep 22 '22 12:09

Kerrek SB


boost::bind and std::bind will copy their arguments so that the returned object contains a copy of each argument, including the function object. If those arguments are expensive to copy then it will be expensive to pass them to std::bind.

You can think about it similarly to creating a tuple of all the arguments, e.g.

auto b = std::bind(func, arg1, arg2, arg3);

should be about equivalent in performance to:

auto b = std::make_tuple(func, arg1, arg2, arg3);

If you don't want the arguments to be copied, use the ref utility to pass them in a reference_wrapper which is a very lightweight type that stores a pointer to the object:

auto b = std::bind(func, std::ref(arg1), arg2, arg3);

When calling the bound function, each of the bound arguments will be passed to the bound function, as lvalues (i.e. no perfect forwarding):

b();  // equiv to std::get<0>(b)(std::get<1>(b), std::get<2>(b), std::get<3>(b))

If the function takes its arguments by value, then the bound arguments will be copied into the function arguments. That could be expensive, but it's exactly the same whether you call the function directly or call it inside the result of std::bind ... it's a property of the function being called, not the bind expression.

So the only overhead of using boost::bind is in the initial copying of the bound arguments, which you can control either by moving the arguments in to avoid copying:

auto b = std::bind(func, std::move(arg1), arg2, arg3);

or passing them by reference:

auto b = std::bind(func, std::ref(arg1), arg2, arg3);

The above discussion ignores the features of bind such as placeholders and calling nested bind expressions, but those do not affect performance and everything above still applies.

like image 37
Jonathan Wakely Avatar answered Sep 22 '22 12:09

Jonathan Wakely