confused with some_var.dup

Rainer Deyke rainerd at eldwood.com
Fri Jan 16 19:32:20 PST 2009


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.

> 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.


-- 
Rainer Deyke - rainerd at eldwood.com


More information about the Digitalmars-d-learn mailing list