[erlang-questions] Arrays and setelement

Mikael Pettersson mikpe@REDACTED
Fri Dec 29 14:28:35 CET 2006

On Fri, 29 Dec 2006 03:11:19 +0000, Joel Reymont wrote:
>> setelement works by copying, except when the BEAM compiler
>> can prove that intermediate copies are redundant, in which
>> cases it simple performs assignments.
>Assignment is what I'm after. How can I help the BEAM compiler prove  
>that intermediate copies are redundant?

AFAIK the BEAM compiler is only able to perform that optimisation
when setelement is applied to a newly created tuple that hasn't
yet "escaped" out of the local context. For example:

A = <make a tuple, either fresh or using setelement>,
B = setelement(N, A, X)

Here the second expression can be converted to use assignment
instead of copying.

The BEAM compiler uses this to optimise tuple and record
initialisation. It may or may not be able to do it in more
general cases; Björn Gustavsson should be able to tell us that.


More information about the erlang-questions mailing list