|
Boost : |
Subject: Re: [boost] [smart ptr] Any interest in copy-on-write pointer for C++11?
From: Ralph Tandetzky (ralph.tandetzky_at_[hidden])
Date: 2013-02-09 04:15:39
On 02/09/2013 07:13 AM, Mathias Gaunard wrote:
> On 08/02/13 22:44, Jeffrey Yasskin wrote:
>
>> I'm sure the WebKit project would welcome a patch demonstrating that
>> flyweights are a more efficient technique for CSS matching. I don't
>> expect
>> they are because these values are built up through several mutations,
>> and a
>> hash table lookup plus a copy on each mutation sounds more expensive
>> than
>> the current copy-on-write system.
>
> I don't pretend to know anything about what WebKit is actually doing.
> I interpreted what you said to mean that they want all values that are
> equal to use the same object. In that case the logical approach is
> indeed to use a flyweight factory.
>
> I think I just misunderstood what this was about; maybe they want to
> do partial COW on subtrees to minimize memory usage for redundant
> information, which is a whole different beast. COW on whole data
> structures is useless, but it is very useful when partial sharing is
> involved. That is not, however, the case that was presented here.
There are many different use cases. Here are two use cases for which I
used cow_ptr so far:
1. Implement an image class that has genuine value semantics. I
explained that in other e-mails before.
2. For a property tree, that looks in essence like this:
class PropertyTree : public AbstractProperty
{
public:
/* implementation of the public interface */
private:
std::list<cow_ptr<AbstractProperty>> properties;
};
Instead of std::list<cow_ptr<AbstractProperty>> you might as well use
cow_ptr<std::list<AbstractProperty>> instead. The result is quite
similar. Note the list of polymorphically behaving elements. Deep copies
are performed properly, if necessary. You can keep a change history of
the property tree in memory easily, since for little changes almost all
the data of the stored trees are shared. At the same time you retain
value semantics. And even "deep copies" are relatively cheap, since only
the pointers in the next layer of the tree need to be copied. I found
cow_ptr<T> quite useful for implementing this.
I would really like to get feedback on the design of cow_ptr<T>
<https://github.com/ralphtandetzky/cow_ptr.git> rather than discussing,
if copy-on-write is a useful pattern in general.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk