question: "Erlang literals are no longer copied when sending messages."

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

question: "Erlang literals are no longer copied when sending messages."

Benoit Chesneau-2
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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: question: "Erlang literals are no longer copied when sending messages."

Jesper Louis Andersen-2
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 <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: question: "Erlang literals are no longer copied when sending messages."

Boshan Sun
Does those literals lives in the constant heap has a ref counter, as the refc binary? If not, how do they get garbage collected? If so, will those constant suffers same "memory leak" issue has refc binary?(a process touched lots of refc binary but never get garbaged collected, so those refc binaries cannot be garbage collected for a long time)

Thanks!

2017-10-26 7:39 GMT-07:00 Jesper Louis Andersen <[hidden email]>:
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 <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: question: "Erlang literals are no longer copied when sending messages."

Sverker Eriksson-4



On 10/26/2017 06:03 PM, Boshan Sun wrote:
Does those literals lives in the constant heap has a ref counter, as the refc binary? If not, how do they get garbage collected? If so, will those constant suffers same "memory leak" issue has refc binary?(a process touched lots of refc binary but never get garbaged collected, so those refc binaries cannot be garbage collected for a long time)


No, literals are not ref counted.

All literals belong to a loaded module instance. When that (old) module instance is purged, all process heaps are scanned and heaps containing those literals will do a special garbage collection where the literals are copied.


/Sverker

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions