[erlang-questions] Accumulator and recursion

Håkan Stenholm <>
Mon Nov 20 02:07:28 CET 2006


Ludovic Coquelle wrote:

> Hi,
> Fairly simple questions about Erlang internal behavior... (I'm learning!)
>
> I have a function which store all the message she get:
> memoryloop(PidToSendResult, Memory) ->
>    receive
>        {store, Data} -> memoryloop(PidToSendResult, [Data|Memory]);
>        stop -> PidToSendResult ! Memory
>    end.
>
> I wonder:
> (1) will the (tail) recursion make a real copy (free/realloc) of data 
> which
> doens't change? I mean if I have not only a Pid to pass but a big state
> configuration list, should I do the same way?
> (2) append an element to a list is supposed to create a new list from the
> previous list state adding the element. Again the same question: is it a
> real copy of all the data? 

No, only a partial list (containg the new elements) needs to be allocated
internaly, as it is safe to point the tail of the new memory block, to the
head of the old list - it's safe as erlang doesn't allow you to change the
content of a "variable" like OldList - a variable is mearly a 
name/reference
for a piece of unchanging data, not a editable memory area.

This is how it works:

                                 -------------------
OldList = [1,2,3,4,5]           | 1 | 2 | 3 | 4 | 5 |    list in memory [*]
                                ^-------------------
                              / |
                              |  -----------------
                              |                   |
                              |  --------------   |
NewList = [a,b,c | OldList]   | | a | b | c | *---   memory allocated 
for NewList
                              |  --------------
                              |
                               ------------
                                           |
                                 --------  |
NewList2 = [foo | OldList]      | foo | *--          memory allocated 
for NewList2
                                 --------


So buidling a "new" list is cheap if it is based on a previous one.

Also note that several list may share parts of their content (elements/list
sequences) with other terms or lists, which may allow you to save memory 
in some
case.

Note: passing data between processes may involve copying, especialy if 
its between
different physical machines, local processes may not do this depending 
on which kind
of memory/GC sheme you use/compile/start erlang with.

[*] : this is actualy a linked list of pointers to erlang terms like; 
1,2,a,foo,
      [1,23], {a,b,foo}


> any hiden optimization? if it is a real copy, is
> it another datastructure to use to implement my function "memoryloop" ?
>
> Thanks!
>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>erlang-questions mailing list
>
>http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list