memory overhead of boost::function<>
Is there an easy way to calculate the minimum and maximum memory overhead required by a single instance of boost::function? It's not easy to trace through the code and find all instances of allocations / deallocations and member variables. sizeof(boost::function<T>) appears to be 32 on my machine for all values of T but I'm not sure if there are certain types of functions which might be less than this or more than this. Is there any way to reduce the memory overhead of boost::function? 32 is quite a bit on an x86 architecture, considering a normal function pointer on the same machine takes only 4 bytes. I was thinking of using boost::function<> as the basis for a generic inter-object messaging system where there may be thousands of objects in the systems any of which might want to communicate via event sinks / sources implemented via boost::function<> and callback registration. But if the memory overhead is 32-bytes per object at a minimum, and possibly more with more complicated function objects or on 64-bit architectures, then it seems like it could quickly grow out of hand if there were many listeners.
Zachary Turner wrote:
It's not easy to trace through the code and find all instances of allocations / deallocations and member variables. sizeof(boost::function<T>) appears to be 32 on my machine for all values of T but I'm not sure if there are certain types of functions which might be less than this or more than this.
AFAIK sizeof(boost::function<T>) is the same for any T.
Is there an easy way to calculate the minimum and maximum memory overhead required by a single instance of boost::function?
Minimum is sizeof(boost::function<T>), maximum is unbounded (as much as your free store can provide).
Is there any way to reduce the memory overhead of boost::function? 32 is quite a bit on an x86 architecture, considering a normal function pointer on the same machine takes only 4 bytes.
It would be possible to reduce the size to one word (4 bytes on x86), but 32 bytes is a better general-purpose solution to store average-sized function objects within the object itself and big-sized ones on the free store. Indeed, keep in mind boost::function is a type erasure mechanism for the callable concept, and as such is a solution for arbitrarily big objects.
I was thinking of using boost::function<> as the basis for a generic inter-object messaging system where there may be thousands of objects in the systems any of which might want to communicate via event sinks / sources implemented via boost::function<> and callback registration. But if the memory overhead is 32-bytes per object at a minimum, and possibly more with more complicated function objects or on 64-bit architectures, then it seems like it could quickly grow out of hand if there were many listeners.
Then maybe you need to build your custom solution. Like if you can guarantee all your function objects are two words max (one for the function, one for the context), then you can have a more optimal solution that is 8 bytes big and never needs the free store. I believe C++0x wants to introduce one to use with lambdas.
participants (2)
-
Mathias Gaunard
-
Zachary Turner