Boost logo

Boost Users :

Subject: Re: [Boost-users] [shared_ptr] Member functions as deallocators
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2009-06-04 02:27:30


On Wed, Jun 3, 2009 at 2:35 PM, Roman Perepelitsa
<roman.perepelitsa_at_[hidden]> wrote:
> 2009/6/3 OvermindDL1 <overminddl1_at_[hidden]>
>>
>> Actually, using Boost.Bind will use a lot more memory overall.  Would
>> it not be better to create a static function somewhere, something
>> like:
>>
>> class SceneManager
>> {
>>    ...
>>    static deallocSM(SceneManage *ptr)
>>    {
>>        ptr->clearScene();
>>    }
>>    ...
>> };
>>
>> And create as:
>> SceneManager *s=fromSomewhere();
>>
>> boost::shared_ptr<SceneManager> p(s, &SceneManager::deallocSM);
>>
>> Doing this means it only keeps a pointer to the function call, instead
>> of through a bind instantiation, meaning this will execute ever so
>> tiny slightly faster, as well as take less space?  It has been a while
>> since I created a new handler like that, but I 'think' the function
>> takes a pointer to the embedded pointer type, check the smart_ptr docs
>> for details I guess.
>
> Modern compiler's might surprise you. Always measure performance or look at
> generated code before resorting to manual optimizations.

Of course, but the bind will, at the very least, keep the function
pointer and the pointer to the class itself, so 2*sizeof(void*),
whereas the smart pointer already passes the pointer to the
deallocation function, meaning there is no reason to duplicate it. In
the best case you are doubling the necessary amount of used memory for
that data structure.


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