[erlang-questions] Cost of Copy
James Hague
james.hague@REDACTED
Thu May 27 20:26:14 CEST 2010
If you ignore message passing, then the copying/sharing issues in Erlang are
just about the same as in Lisp and Scheme, so you may find some information
that way.
The short version is that some values are represented directly in a memory
"cell," like atoms and (most) integers. Others are pointers to data stored
elsewhere (list elements, or conses, which are two consecutive cells in
memory) or tuples (N consecutive cells plus one for the size). Just by
looking at code you can see where conses and tuples are created. If you use
an existing "pointer" to data, then that data is not copied.
Examples:
B = {A, A, A}
This creates a three element tuple (four memory cells). It doesn't matter if
A is a list of a million elements or a tuple or the number 2. B just
contains three cell-sized values (plus the size of the tuple).
X = [57|List]
This creates a single cons (2 cells). The first element is 57. The second is
a pointer to List. So even though you've got two lists (X and List), all of
the data is shared except for the first cons of X.
More information about the erlang-questions
mailing list