Qian Xu wrote:
Denis Koroskin wrote:
The "Hello, World!" string is not allowed to be modified, because it could
be shared throughot the project and will be most probably put in a
read-only memory causing segfault at modification.
But it you need to have a modified version of this this, you create its
copy (duplication, or 'dup' for short) and make whatever changes you want
to it...
This confuses me very.
Do you mean, there is no Copy-On-Write semantic in D?
IMO, D-Compiler should make decision about whether to allocate a new memory
block, not programmer.
You can create a COW array struct pretty easily. However, this will be
pretty slow in a lot of cases.
If you have an array, you're probably going to build it, hold onto it
for a while, and then discard it. You might mutate it in the middle, but
you're probably likely to do a lot of mutations if you do any.
When you're building the array, you really don't want COW semantics.
This will overallocate -- O(n**2) memory required rather than O(n). When
you're mutating large portions, you still don't want COW semantics for
the same reason.
COW is safer, but it can waste resources like nobody's business and only
helps in a few cases. It's better to leave that for a library type.