when is data copied?

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

when is data copied?

Xavier Noria
I would like to have a mental model for data copying in Erlang.

By that I mean immutability is in my view a property of an API, but I suppose the implementation, compiler optimizations, etc., can precisely leverage that property to reuse stuff behind the scenes for performance.

Would you recommend any particular article, talk, book, ..., that would help me get an idea of how that works in Erlang?


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

Re: when is data copied?

Dmytro Lytovchenko
Data is stored in process heap most of the time.
As long as data is moved INSIDE the process, it is NOT COPIED, only a pointer is passed.
If the data ever leaves the process, a message for example, it will be COPIED to its new owner.
If the data is moved to or from ETS, it behaves similarly, it is COPIED (ETS has own heap separate from any process).
If the data is moved between a process and a port, it is COPIED (ports behave similar to processes, and own resources in a similar way)
Large binaries >64 bytes are NEVER copied, instead they are reference counted and reference is shared between processes. This is why you want to GC all processes which touched a large binary periodically, otherwise it may take a long time before the binary is freed.

2017-03-17 11:24 GMT+01:00 Xavier Noria <[hidden email]>:
I would like to have a mental model for data copying in Erlang.

By that I mean immutability is in my view a property of an API, but I suppose the implementation, compiler optimizations, etc., can precisely leverage that property to reuse stuff behind the scenes for performance.

Would you recommend any particular article, talk, book, ..., that would help me get an idea of how that works in Erlang?


_______________________________________________
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: when is data copied?

Xavier Noria
Awesome, thanks!

What about pattern matching?



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

Re: when is data copied?

Dmytro Lytovchenko
Pattern matching is broken into a decision tree during compile time, and this tree is evaluated while doing jumps to corresponding clauses. It is normal Erlang code under the hood, which manipulates Erlang values normally withing current process, so they are handled efficiently.
There's nothing you can optimize in regards to the data copying.

To see how bad is the code you can do benchmarks yourself using timer:tc, thats the simplest. You can compile to intermediate code such as Kernel erlang (shell command c(mymodule, [to_kernel]). ) and read the produced code, or disassemble the module (shell command erts_debug:df(module). )

2017-03-17 11:45 GMT+01:00 Xavier Noria <[hidden email]>:
Awesome, thanks!

What about pattern matching?



_______________________________________________
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: when is data copied?

Xavier Noria
Gotcha, those guidelines are going to be helpful (and they are simple, which is nice!). Thanks Dmytro.

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

Re: when is data copied?

Michał Muskała
I think this article might also be helpful regarding memory layout and data handling: https://www.erlang-solutions.com/blog/erlang-19-0-garbage-collector.html

Michał.

On 17 Mar 2017, 12:02 +0100, Xavier Noria <[hidden email]>, wrote:
Gotcha, those guidelines are going to be helpful (and they are simple, which is nice!). Thanks Dmytro.
_______________________________________________
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