[erlang-questions] Cost of Copy
Richard Andrews
bflatmaj7th@REDACTED
Fri May 28 12:46:50 CEST 2010
On Fri, May 28, 2010 at 6:15 PM, Johan Montelius <johanmon@REDACTED> wrote:
> If we instead have
>
> A = {B, C, D, E, F, G, H }
>
> and want to construct
>
> X = {B', C, D, E, F, G, H}
>
> we construct one new tuple
>
> X = {_, _, _ , _, _, _, _}
>
> and then B'
>
> X = {B', _, _ , _, _, _, _}
>
> so now we have only constructed one tuple of size seven. All other
> structures C,D,E,F,G,H are shared.
but...
IIRC, if A is not used in the code after X is bound, the erlang
compiler can optimise this to an in-place operation where the storage
for A is reused for X. So only the B element pointer is changed to
point to B' and no allocation is required. Is this not one of the
reasons for record syntax?
eg.
A = #myRecord{B,C,D,E,F,G,H},
X = A#myRecord{B2},
More information about the erlang-questions
mailing list