Boost logo

Boost Users :

From: me22 (me22.ca_at_[hidden])
Date: 2006-02-17 19:55:38


On 2/17/06, David Williams <david_at_[hidden]> wrote:
> But what should SomeTypeHere actually be? I've found I can use
> boost::function but would rather not for performance reasons. Any other
> options?
>

Are you sure function is too slow?

- Function object wrapper size
Function object wrappers will be the size of two function pointers
plus one function pointer or data pointer (whichever is larger). On
common 32-bit platforms, this amounts to 12 bytes per wrapper.
Additionally, the function object target will be allocated on the
heap.

- Copying efficiency
Copying function object wrappers may require allocating memory for a
copy of the function object target. The default allocator may be
replaced with a faster custom allocator or one may choose to allow the
function object wrappers to only store function object targets by
reference (using ref) if the cost of this cloning becomes prohibitive.

- Invocation efficiency
With a properly inlining compiler, an invocation of a function object
requires one call through a function pointer. If the call is to a free
function pointer, an additional call must be made to that function
pointer (unless the compiler has very powerful interprocedural
analysis).
~ http://www.boost.org/doc/html/function/misc.html#id2699622

The size of a function is roughly the same as storing a smart pointer
and a member function pointer. Calling through a function pointer is
roughly the same as a virtual function call. I don't think you can
get more speed without losing genericity.

The only possibility I can think of would be something akin to the
mutable-members-in-a-reference-to-temporary trick used in ScopeGuard (
http://www.cuj.com/documents/s=8000/cujcexp1812alexandr/alexandr.htm
), but that's very narrowly applicable.

~ Scott


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net