Rainer Deyke wrote:
Christopher Wright wrote:
You can create a COW array struct pretty easily. However, this will be
pretty slow in a lot of cases.
A built-in COW type does not need to be slow! The compiler can use
static analysis to eliminate unnecessary copies, and reference counting
can be used to further reduce the number of copies.
True. Though you'd need syntax for COW arrays and non-COW arrays. Same
for structs. It makes the language more complicated, and it makes the
compiler even more complicated.
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.
This would not be a problem with a built-in COW type. The compiler can
see that the array is being modified, but not copied, in a block, so it
places a single copy operation at the beginning of the block.
The messy sometimes-a-reference-and-sometimes-a-value semantics of D
arrays are one of the reasons why I still prefer C++ over D.
I agree, but most of the time, I don't modify arrays once they've been
created. If I do modify them a lot, I usually want a set rather than an
array for efficient removals and uniqueness.