
I'd like to use boost::function to wrap (pointer to) function to pass to template functions that expect function objects. I'm just wondering, though, if using boost::function wrapper will introduce additional overhead into the function call? example: template<typename in_t, typename out_t, typename func_t> inline void apply (in_t const& in, out_t out, func_t const&f) { typename boost::range_const_iterator<in_t>::type i = boost::begin (in); typename boost::range_iterator<out_t>::type o = boost::begin (out); for (; i != boost::end (in); ++i, ++o) *o = f (*i); } template<typename in_t, typename out_t> inline void phaseDouble (in_t const& in, out_t &out) { apply (in, out, lvalue_cast (boost::function<typename boost::range_value<out_t>::type (typename boost::range_value<in_t>::type)> (some function))); } }

"Neal Becker" <ndbecker2@gmail.com> wrote
I'd like to use boost::function to wrap (pointer to) function to pass to template functions that expect function objects. I'm just wondering, though, if using boost::function wrapper will introduce additional overhead into the function call?
boost::function will introduce an extra virtual function call (which is not a big deal in most cases). But why do you need to wrap your pointer? Can't you pass it directly? It would be treated exactly the same way as the function object... Regards, Arkadiy

Arkadiy Vertleyb wrote:
"Neal Becker" <ndbecker2@gmail.com> wrote
I'd like to use boost::function to wrap (pointer to) function to pass to template functions that expect function objects. I'm just wondering, though, if using boost::function wrapper will introduce additional overhead into the function call?
boost::function will introduce an extra virtual function call (which is not a big deal in most cases).
OK, thanks.
But why do you need to wrap your pointer? Can't you pass it directly? It would be treated exactly the same way as the function object...
You're right. Sorry for the stupid example. I should probably not post until I wake up.

"Arkadiy Vertleyb" <vertleyb@hotmail.com> wrote in message news:e37oqm$gan$1@sea.gmane.org...
"Neal Becker" <ndbecker2@gmail.com> wrote
I'd like to use boost::function to wrap (pointer to) function to pass to template functions that expect function objects. I'm just wondering, though, if using boost::function wrapper will introduce additional overhead into the function call?
boost::function will introduce an extra virtual function call (which is not a big deal in most cases).
Actually it doesn't if I am not mistaken. Gennadiy

On May 2, 2006, at 12:03 PM, Gennadiy Rozental wrote:
"Arkadiy Vertleyb" <vertleyb@hotmail.com> wrote in message news:e37oqm$gan$1@sea.gmane.org...
"Neal Becker" <ndbecker2@gmail.com> wrote
I'd like to use boost::function to wrap (pointer to) function to pass to template functions that expect function objects. I'm just wondering, though, if using boost::function wrapper will introduce additional overhead into the function call?
boost::function will introduce an extra virtual function call (which is not a big deal in most cases).
Actually it doesn't if I am not mistaken.
boost::function doesn't use virtual functions, but the effect is the same. boost::function adds one additional indirect call through a function pointer. Doug

"Doug Gregor" <dgregor@cs.indiana.edu> wrote
boost::function doesn't use virtual functions, but the effect is the same. boost::function adds one additional indirect call through a function pointer.
Sorry for the inaccurate response. Out of curiousity, is this technique described anywhere (other than the code)? It's hard to imagine the way to erase the type of a functor without some kind of a polymorphic adaptor... Does this technique have an advantage over the one with a virtual function call? Regards, Arkadiy

On 5/2/06, Arkadiy Vertleyb <vertleyb@hotmail.com> wrote:
Out of curiousity, is this technique described anywhere (other than the code)? It's hard to imagine the way to erase the type of a functor without some kind of a polymorphic adaptor...
Does this technique have an advantage over the one with a virtual function call?
Miscellaneous Notes: Performance http://boost.org/doc/html/function/misc.html#id2699622 Miscellaneous Notes: Combatting virtual function "bloat" http://boost.org/doc/html/function/misc.html#id2699664 HTH, Scott McMurray

Arkadiy Vertleyb wrote:
"Doug Gregor" <dgregor@cs.indiana.edu> wrote
boost::function doesn't use virtual functions, but the effect is the same. boost::function adds one additional indirect call through a function pointer.
Sorry for the inaccurate response.
Out of curiousity, is this technique described anywhere (other than the code)? It's hard to imagine the way to erase the type of a functor without some kind of a polymorphic adaptor...
FWIW, I also had the same curiosity and then roughly discribed the comparison of virtual way between Boost.Function way: http://tinyurl.com/ntf7x -- Shunsuke Sogame

"Arkadiy Vertleyb" <vertleyb@hotmail.com> writes:
"Neal Becker" <ndbecker2@gmail.com> wrote
I'd like to use boost::function to wrap (pointer to) function to pass to template functions that expect function objects. I'm just wondering, though, if using boost::function wrapper will introduce additional overhead into the function call?
boost::function will introduce an extra virtual function call (which is not a big deal in most cases).
Actually it's not a virtual call, it's a call through a function pointer. That's one less indirection. -- Dave Abrahams Boost Consulting www.boost-consulting.com

David Abrahams wrote:
"Arkadiy Vertleyb" <vertleyb@hotmail.com> writes:
"Neal Becker" <ndbecker2@gmail.com> wrote
I'd like to use boost::function to wrap (pointer to) function to pass to template functions that expect function objects. I'm just wondering, though, if using boost::function wrapper will introduce additional overhead into the function call?
boost::function will introduce an extra virtual function call (which is not a big deal in most cases).
Actually it's not a virtual call, it's a call through a function pointer. That's one less indirection.
Do you think a smart compiler might optimize this to a direct call?

On May 2, 2006, at 1:01 PM, Neal Becker wrote:
David Abrahams wrote:
Actually it's not a virtual call, it's a call through a function pointer. That's one less indirection.
Do you think a smart compiler might optimize this to a direct call?
I have yet to see a compiler smart enough to optimize this indirection. Doug

Doug Gregor wrote:
On May 2, 2006, at 1:01 PM, Neal Becker wrote:
David Abrahams wrote:
Actually it's not a virtual call, it's a call through a function pointer. That's one less indirection.
Do you think a smart compiler might optimize this to a direct call?
I have yet to see a compiler smart enough to optimize this indirection.
IOW, the overhead might be *significant* for small functions that would otherwise be inlined (and insignificant for larger functions). -Thorsten

Thorsten Ottosen wrote:
Doug Gregor wrote:
On May 2, 2006, at 1:01 PM, Neal Becker wrote:
David Abrahams wrote:
Actually it's not a virtual call, it's a call through a function pointer. That's one less indirection.
Do you think a smart compiler might optimize this to a direct call?
I have yet to see a compiler smart enough to optimize this indirection.
IOW, the overhead might be *significant* for small functions that would otherwise be inlined (and insignificant for larger functions).
Exactly. I've done some tests in the past trying to quantify the cost of virtuals, direct functions (inlined and not) and indirect functions. The cost of an indirect function is in practice not distinguishable from a non-inlined direct function. A virtual is (very) slightly costlier. The real difference is in inlining. -- Giovanni P. Deretta

Doug Gregor wrote:
On May 2, 2006, at 1:01 PM, Neal Becker wrote:
David Abrahams wrote:
Actually it's not a virtual call, it's a call through a function pointer. That's one less indirection.
Do you think a smart compiler might optimize this to a direct call?
I have yet to see a compiler smart enough to optimize this indirection.
Curious question: Say we use a functor class template adapter with a function pointer as non-type template parameter... In case we pass it to function templates that treat functors uniformly (such as std algorithms), the compiler has (at least theoretically) a fair chance to make an absolute call or even to inline. Boost.Function, however, implements distinct handling for function objects and function pointers. So, is there any chance for reducing the work at the call site with this technique? Thanks, Tobias

On May 2, 2006, at 2:46 PM, Tobias Schwinger wrote:
Curious question:
Say we use a functor class template adapter with a function pointer as non-type template parameter...
In case we pass it to function templates that treat functors uniformly (such as std algorithms), the compiler has (at least theoretically) a fair chance to make an absolute call or even to inline.
Boost.Function, however, implements distinct handling for function objects and function pointers. So, is there any chance for reducing the work at the call site with this technique?
Sure. When you stick a function pointer into a boost::function, you actually get two indirect calls for each invocation: the first goes from the boost::function::operator() to a "manager" that encodes the type of the function pointer, and the second goes through the function pointer to the function. If you encode the function pointer in the type of the function object that you stick into boost::function, the compiler can eliminate the second call. Doug
participants (10)
-
Arkadiy Vertleyb
-
David Abrahams
-
Doug Gregor
-
Gennadiy Rozental
-
Giovanni P. Deretta
-
me22
-
Neal Becker
-
Shunsuke Sogame
-
Thorsten Ottosen
-
Tobias Schwinger