6
votes

As I've understood, when you create a new list with expression like the following, Erlang doesn't copy L1, it just copies H.

L2 = [H|L1]

Does Erlang have persistent a data structure (see Persistent data structure) for dict, that is, when you add/remove/modify nodes in the tree only few elements are being copied (like in Clojure)?

2

2 Answers

12
votes

You have misunderstood the situation when you build a list using [H|T]. It is as you say that T is not copied but neither is H. All that happens is that a new list cell is prepended to T with a reference to H as its head (its tail is T). When working with lists the only bits which are created are the actual list cells and never the data in each cell.

The same happens when working with dict. When you modify (add/delete elements) in the dict only the actual dict structure is modified and not the actual data in the dict. Also it is smart so as to only copy as little of the dict structure as is necessary to make the modification.

So, yes, Erlang has persistent data structures. In that respect clojure is like Erlang (we were around long before it).

2
votes

In my experience, the data structures for the library module do not degrade in performance or memory pressure when they get larger.

For a dict, it uses a dynamic hash table as internal data structure and work is done essentially only on the bucket where the modification is done.

I also looked in the gb_trees module where I found the comment:

Behaviour is logaritmic (as it should be).

And gb_trees are generally pretty fast, so I'm quite sure not much copying is going on.

Generally, if you implement data structures like these in a language like Erlang, you take care of copying issues, so there is no need to worry about it for the general library functions.


I reread the article about persistent data structures: in the sense of this article, Erlang's data structures are fully persistent and also confluently persistent.