|
Boost : |
Subject: Re: [boost] [function] function wrapping with no exception safetyguarantee
From: Daniel Walker (daniel.j.walker_at_[hidden])
Date: 2010-10-08 11:56:41
On Thu, Oct 7, 2010 at 9:22 PM, Peter Dimov <pdimov_at_[hidden]> wrote:
> Daniel Walker wrote:
>
>> void do_stuff(boost::function<int(int)> f)
>> {
>> // ...
>> if(f) {
>> // Remove the exception safety guarantee for this block.
>> boost::unsafe_function<int(int)> g = f;
>> for(int i = 0; i < INT_MAX; ++i)
>> g(i);
>> }
>> // ...
>> }
>
> This example implies that unsafe_function is inherently more efficient, but
> this need not be so. An empty function may point to throw_bad_function_call
> instead of NULL.
I don't believe efficiency is a significant issue. I tried to measure
the difference in performance between boost::function and
unsafe_function in a tight loop, and with gcc -02, the difference is
on the order of hundreds of picoseconds; i.e. in optimized code on
contemporary PC hardware, unsafe_function is not significantly more
efficient. But of course, results will vary according to your
optimizer.
The main advantage I see in unsafe_function is that it could be useful
in environments where throw_bad_function_call and/or compiler
optimizations are unavailable, such as in some embedded systems.
Daniel Walker
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk