[erlang-questions] question: "Erlang literals are no longer copied when sending messages."

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Thu Oct 26 16:39:07 CEST 2017

The compiler detects literal constants. A function such as

f() -> ["I", "am", "a", "literal", "constant", "valiue"].

will return a constant list. These lists go into a "constant heap" which
means that other heaps can just reference those values directly from there.
Prior to Erlang version 20.0, if you sent such a constant (locally) from
one process to another, the send would copy the constant literal into the
mailbox of the receiving process. Not anymore. The value is referenced from
the constant heap. There is a subtlety when you load new code in which the
old constants are correctly retained by the VM, but otherwise, things works
seamlessly for programmers.

Your example is somewhat degenerate because the empty list and empty map
already has special representation in the VM. One way to detect if a value
is a constant is to look at the output from the compiler. The module will
have a special section for constant literals, and you can verify that the
compiler indeed did detect a value as being a constant. In general,
constants are congruences in the sense that compounding structures are
constants if all their children are in the term tree.

On Thu, Oct 26, 2017 at 1:18 PM Benoit Chesneau <bchesneau@REDACTED> wrote:

> With Erlang 20, "Erlang literals are no longer copied when sending
> messages" according to the changelog.
> Does it means something like #{ somelist => [], map => #{ } } will be
> returned from a gen_server call without beeing copied?  (ie the content of
> somelist and somemap will only be referenced ?
> Benoit
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171026/b48c27e1/attachment.htm>

More information about the erlang-questions mailing list