Immutable data not copied

Tomek Sowiński just at ask.me
Wed Dec 9 14:29:02 PST 2009


Dnia 09-12-2009 o 09:54:33 Lutger <lutger.blijdestijn at gmail.com>  
napisał(a):

> Tomek Sowiński wrote:
>
>> const(FAQ) says: "When doing a deep copy of a data structure, the
>> invariant portions need not be copied."
>> I'm trying to imagine what code would benefit from such optimization.
>>
>> immutable struct Large { whole lotta data... }
>>
>> struct Other { Large l; }
>>
>> void funkcja(Large s);  // no reference annotation on parameter, copied?
>> {
>>     Large t = s;   // copied?
>>     auto other = Other(s);  // copied?
>>     Other o = other;  // other.l copied?
>> }
>>
>>
>> Tomek
>
> As Simen kjaeraas said, deep copy is about following references. The
> optimization mentioned in the faq is not something done automatically by  
> the
> compiler, but merely made possible by immutable. Here is a naive example:
>
> class ImageEditDocument
> {
>     immutable(Image)[] layers;
> }
>
> Making a copy of such a document for further editing would normally  
> require
> copying all images in the layers array. If they are immutable, you can  
> just
> copy the references because you know the images will never be modified.

I see, thanks. The point to remember is that you need reference semantics  
(classes, arrays) to dodge copying gracefully.


Tomek


More information about the Digitalmars-d-learn mailing list