digitalmars.D.learn - Immutable data not copied
- =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= (12/12) Dec 08 2009 const(FAQ) says: "When doing a deep copy of a data structure, the
- Simen kjaeraas (8/20) Dec 08 2009 d?
- Lutger (11/28) Dec 09 2009 As Simen kjaeraas said, deep copy is about following references. The
- =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= (11/42) Dec 09 2009 ed?
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
Dec 08 2009
On Wed, 09 Dec 2009 01:18:46 +0100, Tomek Sowi=C5=84ski <just ask.me> wr= ote: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, copie=d?{ Large t =3D s; // copied? auto other =3D Other(s); // copied? Other o =3D other; // other.l copied? } TomekDeep copy means follow references and pointers, so it refers to referenc= es = and pointers, not POD. -- = Simen
Dec 08 2009
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? } TomekAs 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.
Dec 09 2009
Dnia 09-12-2009 o 09:54:33 Lutger <lutger.blijdestijn gmail.com> = napisa=B3(a):Tomek Sowi=F1ski 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.=ed?immutable struct Large { whole lotta data... } struct Other { Large l; } void funkcja(Large s); // no reference annotation on parameter, copi=by ={ Large t =3D s; // copied? auto other =3D Other(s); // copied? Other o =3D other; // other.l copied? } TomekAs Simen kjaeraas said, deep copy is about following references. The optimization mentioned in the faq is not something done automatically =the compiler, but merely made possible by immutable. Here is a naive examp=le: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 semantic= s = (classes, arrays) to dodge copying gracefully. Tomek
Dec 09 2009