compiler question ?

Kent Boortz <>
Thu Oct 12 00:40:12 CEST 2000


> cool.and what about lists or tuples?
> 
> f({A,B})->{g(A),B}. is B copied ?
> 
> g([H¦T])-> [k(H) ¦ T]. is T copied ? ( forget about tail recursiveness, can 
> unmodified part of list be not copied ?

You can check this yourself, 

  % erlc -S test.erl

produces a file "test.S" that is not that hard to read (I think
some optimizations are done at load time but the code is close
to what is executed).

In the *.S file the "allocate" and "deallocate" makes space on the
stack, a 'y' variable is located on the stack. From this we can see
that 'B' is stored on stack as well as 'T'.

When you write "copied" I assume you mean one cell (32 bits) needed
for storing the object or a pointer to a larger object on the
stack. The object itself is not copied in the examples above. It is
rare that Erlang data is copied, the main occasions are when sending
messages or storing data into ETS tables.

But there are still some optimizations missing in the compiler that
cause some "copying". It will not transform constructs consuming heap
space like

  h({A,B}) ->
      k({A,B}).

to something equivalent that is not using heap space

  h({A,B} = X) ->
      k(X).

kent




More information about the erlang-questions mailing list