Does the copy and update procedure for immutable records in F# share or copy memory? Meaning, in the following code
type MyRecord = {
X: int;
Y: int;
Z: int
}
let myRecord1 = { X = 1; Y = 2; Z = 3; }
let myRecord2 = { myRecord1 with Y = 100; Z = 2 }
do myRecord1
and myRecord2
share memory for the variable X
? More generally, is there a good reference that denotes exactly which immutable/persistent data structures in F# actively share memory?
In this case, the memory for variable X
will be copied. The last line of your code is really just another way of writing this:
let myRecord2 = { X = myRecord1.X; Y = 100; Z = 2 }
Now, if X
was of a reference type, the memory for reference to it would have been copied, but the memory for its contents would have been shared.
For example, consider this:
type MyX = { W: int; U: int }
type MyRecord = {
X: MyX;
Y: int;
Z: int
}
let myRecord1 = { X = { W = 5; U = 6 }; Y = 2; Z = 3; }
let myRecord2 = { myRecord1 with Y = 100; Z = 2 }
In the above code, the memory for X
will be copied, but memory for W
and U
will be shared.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With