Sign in to follow this  
Anton Vatchenko

Best callback technique

Recommended Posts

What is the best practice for C++:
1. Static pointers.
2. Templates (functors).
3. Interfaces (usually virtual inheritance).

Best means:
1. Good code style (static functions are non-stable in casting for wrong instances passed).
2. Good speed (if there is about thousand callbacks per frame).

Share this post


Link to post
Share on other sites
SiCrane    11839
Theres no such thing as a single best method for doing callbacks. If you're worried about performance, profile the different techniques yourself.

Also, in the future don't bump your threads when less than two hours have passed. The general programming FAQ lists the time before a bump to be twenty four hours.

Share this post


Link to post
Share on other sites
Eddycharly    437
i would say generated free functions by templates are a good solution (because free functions pointers have a fixed size), that's not trivial to write such code though.

interfaces is probably the simplest method but it is not very flexible imo.

Share this post


Link to post
Share on other sites
Antheus    2409
Quote:
Original post by Anton Vatchenko
2. Good speed (if there is about thousand callbacks per frame).


This type of problems is better solved using queues. Rather than invoking callbacks, have a queue of results. Given an example of collision detection:
struct CollisionPair {
Foo * a;
Foo * b;
};

vector<CollisionPair> results;

find_collisions(data, results);

handle_collisions(results);


Quote:
What is the best practice for C++:

boost::function. Solves all the millions of edge cases involved.

Share this post


Link to post
Share on other sites
I wrote a simple tests for functors (templates), interfaces and statics. In most 2-processor and huge_gb-RAMs static functions are 0ms per 100 million callbacks, meanwhile interfaces (with virtual diamond-like inheritance) look like the slowest ones. But on old machines static functions look like the slowest.

Share this post


Link to post
Share on other sites
Antheus    2409
Quote:
Original post by Anton Vatchenko
I wrote a simple tests for functors (templates), interfaces and statics. In most 2-processor and huge_gb-RAMs static functions are 0ms per 100 million callbacks
That just means compiler optimized them out.

Dispatching 100 million NOP callbacks will take on the order of 100ms under ideal circumstances.

Real messaging systems can handle in the order of 100k/second.

Share this post


Link to post
Share on other sites
speciesUnknown    527
My favourite method is the interface based callback - its cheap and easy to implement. boost signal / slots are a more powerful alternative if you like to bind individual functions as the callback receivers instead of a whole class.

Share this post


Link to post
Share on other sites
Decrius    100
Boost Signal / Slots is a very good solution on all fronts, but it's speed might be lacking for you. For my specific tests, SigC++ was twice as fast (but measure this for your particular use!). I wrote my own limited kind of signal/slots, which is again 3 times faster, roughly.

So, check for yourself. Boost is truely not a fast solution for signals/slots, but it sure is stable, compatible, etc.

Share this post


Link to post
Share on other sites
alvaro    21246
Quote:
Original post by Decrius
Boost Signal / Slots is a very good solution on all fronts, but it's speed might be lacking for you.


Quote:
Original post by Anton Vatchenko
2. Good speed (if there is about thousand callbacks per frame).


I can't imagine a thousand callbacks per frame being a performance problem.

Share this post


Link to post
Share on other sites
alvaro    21246
Quote:
Original post by Anton Vatchenko
if you imagine about 200 frames per second, and this callback system is not the main target in application.


In a quick test on my computer, it looks like the overhead would take about 3% of the CPU under those circumstances. But such a simple synthetic test doesn't mean much, so you will have to do your own profiling.

We can't help you decide what to use: We can only point out alternatives for you to try. If you can budget boost::signals, they are a very elegant way to do things. An old-fashioned function pointer is much much faster, so I am sure there will be some speed/elegance compromise that you can use.

Share this post


Link to post
Share on other sites
Antheus    2409
Quote:
Original post by Anton Vatchenko
if you imagine about 200 frames per second, and this callback system is not the main target in application.


boost::function is fine then. It's ISO certified up to 220 (160 in Imperial units) frames per second.

Share this post


Link to post
Share on other sites
doesnotcompute    262
Quote:
Original post by Antheus
Quote:
Original post by Anton Vatchenko
if you imagine about 200 frames per second, and this callback system is not the main target in application.


boost::function is fine then. It's ISO certified up to 220 (160 in Imperial units) frames per second.


LOL!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this