I just saw this nice copy-on-write pointer implementation. It looks pretty generic and useful, so my question is: Is such a class contained in any of the C++ toolkits (boost, loki, etc.)? If not, I'd really like to know why because it is a really useful idiom and apparently a generic implementation seems doable (like the one I linked to).
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
There a contradiction between "pointer" and "copy on write": by definition, dereferencing a pointer doesn't change it, and changing
*p
doesn't changep
.So a const pointer can be dereferenced, and the resulting lvalue can be modifiable.
You are really talking about a one-element container, not a pointer.
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:
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:
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 ?
There was a lot of debate over the possibility, and at least one suggested version of what eventually came out as
auto_ptr
was for a reference counted COW pointer.Unfortunately, the time for COW has mostly passed. Making a COW pointer (or COW-whatever) thread-safe can introduce serious performance problems.
Edit: Rereading that, I feel obliged to point out that not all use of COW necessarily obsolete. There are times that it still makes sense. The overhead of a thread-safe increment is pretty much fixed -- so it's only a question of how large an object has to be, or how expensive it is to copy, for COW to make sense. There are also times/places that you have lots of copies of an (unmodified) object, and the savings in memory can be a reasonable tradeoff -- the savings in memory justify some extra processor time. If you can save paging a little data to/from disk, you can come out ahead in a hurry.