Efficiency of copying values

141 views
Skip to first unread message

Evan Zamir

unread,
Dec 15, 2014, 9:08:40 PM12/15/14
to elixir-l...@googlegroups.com
Clojure uses persistent data structures to improve the efficiency of copying lists and such. I'm wondering if Elixir employs a similar approach or is the copy in Elixir a very expensive process?
-evan

Stuart Coyle

unread,
Dec 15, 2014, 9:36:00 PM12/15/14
to elixir-l...@googlegroups.com
This is really dependent on the Erlang VM. Maybe read: http://www.erlang.org/doc/efficiency_guide especially the part on processes: how and when
messages are copied. 

On Tue, Dec 16, 2014 at 1:08 PM, Evan Zamir <zamir...@gmail.com> wrote:
Clojure uses persistent data structures to improve the efficiency of copying lists and such. I'm wondering if Elixir employs a similar approach or is the copy in Elixir a very expensive process?
-evan

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Stuart Coyle
stuart dot coyle at gmail dot com


José Valim

unread,
Dec 16, 2014, 4:17:48 AM12/16/14
to elixir-l...@googlegroups.com
It is specific to data type.

Tuples are allocated contiguously in memory. This means adding, removing or updating an element copies the whole tuple. This is ok (and often the most performant way) if you keep your tuples small.

Lists are linked lists. Prepending an element does not perform any copy, appending copies the whole list. After all, a linked list is a degenerate tree.

HashDict and HashSet are implemented similar to their Clojure variants.

Maps today copy the whole structure, unless you are updating an existing key, meaning the key space is shared and the value space is copied (as a tuple). The OTP team is working on large maps which should be available in upcoming Erlang versions which will likely use a tree approach (as in Clojure) once you reach a number of keys (12 or 16).

======

As Stuart pointed out, it is worth noting that sending messages copies the data structures, unless it is a large binary. The Erlang VM also performs many tricks to avoid copying in many places. For example, matching on a binaries creates references to the larger binary instead of copies. Multiple operations in a tuple does not copy it n-times, but only once if nothing is using the intermediate result. Data structure literals in your code live in a constant pool and are not copied at any time, etc.



José Valim
Skype: jv.ptec
Founder and Lead Developer

On Tue, Dec 16, 2014 at 3:08 AM, Evan Zamir <zamir...@gmail.com> wrote:
Clojure uses persistent data structures to improve the efficiency of copying lists and such. I'm wondering if Elixir employs a similar approach or is the copy in Elixir a very expensive process?
-evan

--

Evan Zamir

unread,
Dec 16, 2014, 11:49:15 AM12/16/14
to elixir-l...@googlegroups.com, elixir-l...@googlegroups.com
Wow thanks for the thorough answer Jose. Appreciate your hard work and will keep digging into Elixir. Cheers,
Evan



You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-talk/hdT1S7Awb88/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-ta...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages