In an environment restricted to C++03,boost::signals2 was used alongside boost::function and boost::bind to implement a simple messaging system between components. It works great and I have no problems with it whatsoever.
However, in another environment where C++11 is supported fully, is boost::signals2 overkill for very simple applications?
To clarify, by simple I mean the following:
void
Performance is key in this application, so all the wonders of boost::signals2 that the application doesn't need could be doing more harm than good.
Right now, there is simply an std::vector<std::function> handling this, and switching over to something else such as boost::signals2 would be very simple to do if it was deemed suitable.
I recently came across the same question for a project with similar simple requirements and did some research that I'd like to share here.
My main interest was in the overhead of boost::signals2 compared to a simple vector-of-callback-functions implementation. Finally I did some more research as shown in the following table:
Benchmark Duration (normalized)
---------------------------------------------------------------------
Direct function call 1
Function pointer call 1
Virtual function call 1
std::function call 1.5
std::vector<std::function> call 2
boost::signals signal invocation 78
boost::signals2 signal invocation (dummy mutex) 43
boost::signals2 signal invocation 92
The measurements have been taken on a Ubuntu 15.04 with gcc 4.9.2, optimization -O2, Boost v1.55. Because the absolute values might well be meaningless outside of my box the values are normalized. More recent versions of Boost might well be faster.
My conclusion would be: If performance is critical and you don't need thread safety (or other advanced features) reconsider using boost::signals2.
If you want to reproduce the measures on your machine the code is available here.
To conclude the conversation had in the comments:
It seems that this question has boiled down to, stop theorizing, start benchmarking
In the end, I found the some of the additional features of boost::signals2 were highly beneficial (such as .track on slots) and worth any performance cost, if any, they incurred.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With