Boost logo

Boost :

Subject: Re: [boost] copy on write for std containers
From: Simonson, Lucanus J (lucanus.j.simonson_at_[hidden])
Date: 2010-12-04 17:53:11


Peter Foelsche wrote:
> I wrote a couple of lines and replaced std::set and std::map with some
> copy-on-write-wrappers
> and I got a dramatic speed improvement with the last gcc 4.5. I think.
>
> My code was using recursive functions iterating over a tree returning
> and setting such objects.
>
> I looked and it seems there is nothing like this in boost!
>
> Ideally this should be an template argument for the std containers.

No. The problem you have solved is not general and furthermore it is not the right solution. Rather than pass containers by value and return by value it is standard practice to pass by const reference and make a copy when you are aware that you want to modify a copy. To return a container pass it by non-const reference and populate it, which avoids copy on return. Finally there is iterator semantics which also allow efficient usage of the STL. The solution to this performance problem is to use the STL correctly in the first place. Your code would be faster still without the extra level of indirection and extra memory allocations of the wrapper.

Regards,
Luke


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk