TP.
TP.

Reputation: 750

In Erlang, what are the benefits of using ets instead of process dictionary within a process?

I think using ets will still introduce similar side effects.

Upvotes: 11

Views: 6112

Answers (4)

tzp
tzp

Reputation: 552

No doubt that ETS has much more functionality and is more sophisticated. But...

  • As the process dictionary update/lookup operations move only the references, not the whole data (see the accurate answer by Christian), it can be much faster, especially for big data structures. Once I refactored a part of code to keep the big and frequently accessed data structures in proc. dict instead of ETS and we had a 30% speedup in that part of the code.

  • In many cases the data is accessed only by a single process. In that case I don't see a big theoretical difference between the proc.dict and ETS. Both serve for keeping side effects in memory. Besides, you can access the proc.dict of another process like

    process_info(whereis(net_kernel),dictionary).

Upvotes: 6

rvirding
rvirding

Reputation: 20916

ETS more or less behaves as if the table was in a separate process and requests are messages sent to that process. While it is not implemented with processes that the properties of ETS are modeled like that. It is in fact possible to implement ETS with processes.

This means that the side effect properties are consistent with the rest of Erlang.

The process dictionary is like nothing else in Erlang and adding it was a big mistake. There is no reason to use the process dictionary instead of one of the process local dictionaries like dict or gb_trees.

Upvotes: 13

Christian
Christian

Reputation: 9486

ETS is not garbage collected since it is stored in a heap outside of erlang processes. This means that when you put something into ets it is copied into it, and when you take it out, you get a copy in your process. Making lots of ets lookups can then lead to excess consing in your process (but this is only relevant for very high througputs).

The process dictionary is garbage collected. It is stored in the process's own heap. So when you look things up in it you get a reference to the exact same value you put in it. The values stored in the process dictionary are not compacted.

Both approaches are non-pure, i.e. they have side-effects. Yes it is bad, and yes it is not why we have both alternatives.

Upvotes: 16

Zed
Zed

Reputation: 57658

Benefits of ETS over process dictionary are:

  • Other processes can access the ETS table directly
  • ETS gives you searching / matching / iteration facilities, while the process dictionary is only a key-value store.
  • You can save/load tables to files in one step
  • If your owner process dies the table can be inherited by someone else, so the data is not lost.

Upvotes: 11

Related Questions