|
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-08 16:22:23
On 02/08/2013 09:28 PM, Mathias Gaunard wrote:
> On 08/02/13 18:35, Jeffrey Yasskin wrote:
>> WebKit has a class vaguely like this for your case #2:
>> https://code.google.com/p/chromium/codesearch/#chrome/src/third_party/WebKit/Source/WebCore/rendering/style/DataRef.h
>>
>> used at
>> https://code.google.com/p/chromium/codesearch/#chrome/src/third_party/WebKit/Source/WebCore/rendering/style/RenderStyle.h&rcl=1360310731&l=137.
>> Semantically every copy is a real copy, and, contrary to Mathias'
>> assertion, couldn't be replaced by a move in C++11, but they want to
>> share identical values when doing so is cheap.
>
> If you want identical values to use the same resource, you should use
> a flyweight factory.
>
A flyweight factory has different use cases:
- Normally there is no reference count in a flyweight factory or their
objects. Hence, when you want to modify an object, then a copy is made
in every case, even if there is only one copy. Therefore, we can see,
that a flyweight factory does usually not implement copy-on-write and
can be less efficient in the case that one single object is modified
regularly.
- If there's a single factory in a multi threaded environment, then
synchronization can be a real bottleneck. This is not the case for
cow_ptr<T> which works lock free.
Hence flyweight factories have different use cases and performance
characteristics. COPY-ON-WRITE HAS ITS OWN VALID USE CASES. A cow_ptr<T>
template class can be utilized to implement copy-on-write for different
situations.
With cow_ptr<T> come some other nice use-cases such as
- using it as a pimpl pointer without the need to implement copy and
move constructors and assignments or the destructor,
- adding cloning to a type hierarchy non-intrusively and
- building arrays of polymorphic types with value semantics.
(See the file cow_ptr.h in the github repository
<https://github.com/ralphtandetzky/cow_ptr.git> for details.) Sometimes
these use-cases mix. I've seen it in my own production code. Believe me,
there are legit use-cases of cow_ptr.
My questions are:
1. Would this fit into the boost libraries?
2. What improvements can I make to the current design and
implementation? I would really appreciate constructive feedback.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk