I am currently working on network software. It has one main class, server
which obviously represents a server instance.
A server
instance can send requests and the user is notified of the response by a callback.
The code is like:
class server
{
public:
typedef boost::function<void (int duration)> callback_func;
void send_request(endpoint& ep, callback_func cb);
};
Now let's say that, as a user I want to the callback to know about the instance that called it, I can do the following thing:
void mycallback(const server& sv, int duration) { ... }
server sv;
sv.send_request("localhost", boost::bind(&mycallback, boost::ref(sv), _1));
But I wonder: is there any overhead doing so ? Will the calls to mycallback
be any slower than using a "regular" call ?
Thank you.
Foot note: I could of course change my typedef
to: typedef boost::function<void (const server& sv, int duration)> callback_func;
and if boost::bind
cause any significant overhead, that's probably what I will do in the end. I would just like to know what costs implies the use of boost::bind
.
Of course it causes overhead. What it does is that it creates a functor that stores the bound parameters and has an operator()
that you call with the remaining arguments. Now, is this significant? I don't know, because that is just a word. Make 10 million requests and measure it. You are the only one who can tell if this overhead is significant to you or not.
Also, I faced a similar problem. Since I didn't really need delegates, I could get away with function pointers. But I found an interesting benchmark which also mentions some alternative implementation, all of them with better performance than boost::function
. This comes at the cost of portability and in some cases ugly, non-standard hacks in the implementation (but a really good performance in return).