[erlang-questions] Question about implementation

Lukas Larsson <>
Mon Sep 10 18:08:26 CEST 2012


Hello,

Only the tuple which is the record and the immidiates (i.e. small int,
atom, pid, ref etc) will be copied. All other parts use pointers
internally and the same pointers will be copied into the correct
places within the tuple. So in this case a new 6 word memory block
will be created and filled with the atom undefined and finally the new
counter.

I think that the compiler does some optimization based on updating
tuples within the same function body, though I've forgotten what it is
it does.

Hope that makes sense.

Lukas

On Mon, Sep 10, 2012 at 4:27 PM, Max Lapshin <> wrote:
> For example I have such gen_server:
>
> -record(state,
>    field1,
>    field2,
>    field3,
>    counter = 0
> }.
>
>
>
> handle_info(incr, #state{counter = Counter} = State) ->
>    {noreply, State#state{counter = Counter + 1}};
>
>
> Will here whole #state{} be deep-copied with new Counter +1 field, or
> it is possible to guess, that here optimization is possible and just
> "edit" counter field in existing tuple?
>
> Is it possible to understand by erlang source code, that State is no
> longer referenced anywhere to avod large allocation and free?
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list