From: David Maisonave (dmaisonave_at_[hidden])
Date: 2006-01-04 12:21:07
"Thorsten Ottosen" <tottosen_at_[hidden]> wrote in message
> Thorsten Ottosen wrote:
> > axter wrote:
> >>This is a continuation on a thread discussion advocating replacing
> >>the boost pointer containers with cow_ptr as the default pointer
> > Test performance for initializing and copying container of pointers.
> > vector<copy_ptr<Shape> > 0.42 s
> > boost::ptr_vector<Shape> 0.42 s
> > vector<cow_ptr<Shape> > 0.09 s
> > >>>>>>>>>>>>>>>>>>
> > cow_ptr seems to win here. I can't figure out why cow_ptr is so much
> > faster. ptr_vector uses cloning, of course, whereas cow_ptr does
> > something else.
> I further tried to add the proper return type to the functions, such
> that ptr_vector<T> returns by auto_ptr< ptr_vector<T> > and vector<T>
> vector<T>. On top of this, I save the return-value and called size()
> This gave:
> Test performance for initializing and copying container of pointers.
> vector<copy_ptr<Shape> > 1.42 s
> boost::ptr_vector<Shape> 0.81 s
> vector<cow_ptr<Shape> > 0.13 s
> But this test is a bit unfair: we create copies of the vector, but we
> never mutate that copy. A fair test would mutate all the elements.
The cow_ptr is like a clone pointer and a shared pointer put together.
It shares the pointer, until an attempt is made to access it via
non-constant operator-> or operator*.
If non-constant access is made, then it checks to see if it's reference
counter is larger then one. If so, it then performs the deep-copy, and
releases the reference count.
This is very efficient, because there's a good chance that the using
code will never access all or any of the container objects in a
The difference in efficiency will show up even more, when you have a
more complex type that takes more time to construct. The test code uses
a simple Shape class, which doesn't have much to it, but if you added
more data members, and made the construction more complex, then the
above test will be drastically different and it would show cow_ptr
having a very high performance ratio.
By the way, I believe you could add this type of COW logic to the
existing boost pointer containers, and increase their efficiency.
But there is an overhead to this logic, and you might have notice that
some of the test show copy_ptr out performming cow_ptr.
You could consider adding a policy option to the boost pointer
containers, that would allow choosing between COW method and
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk