Like Jerry Coffin said it s been demonstrated that the COW idiom introduced performance issues... but there is actually another issue.
It is not possible (as demonstrated in the very article you link to) to actually write a generic implementation of COW. In the COW implementation of std::string
the Copy is performed whenever an operation is invoked that will actually modify the state of the string. However, how is a pointer supposed to know that ? It has no knowledge over the class it points to.
For example, let s assume I do this:
void method(Foo& foo, flag_t flag)
{
if (flag == flag::Yes) foo.modify();
}
void invoke(COWPointer<Foo> ptr)
{
method(*ptr, flag::No);
}
Oups! I make a copy of the Foo
object even though it s not going to be modified!
The problem is that while this COW class helps, you actually has to wrap it:
class Foo
{
public:
private:
COWPointer<FooImpl> mImpl;
};
And then methods of Foo that really modifies the object will be responsible for copying the FooImpl
state. So sure the class helps, but it s not silver bullet either.
And all this trouble... without being sure of actually gaining performance because of the synchronization issues in a MT application...
Is it not simpler to actually avoid copying whenever possible (using references / pointers) rather than tweaking your class for a possible gain in some situations that will penalize users that already took care of performances issues ?