tags:

views:

554

answers:

3

I think using ets will still introduce similar side effects.

+4  A: 

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.
Zed
+10  A: 

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.

Christian
Almost all Erlang books say we should stay away from process library but I think using ets is not much different.
TP
"Almost all Erlang books"... does that mean two out of three? =)
Zed
+7  A: 

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.

rvirding
I guess a difference between ETS and the "process-based ETS" would be that the former can be accessed truly concurrently when using fine locks, while the latter will always enforce synchronization (a.k.a. serialization).
Zed
Not really from one process as accessing ETS is synchronous so the "calling" process has to wait till it gets a "reply" from ETS. The difference is that the process dictionary will always be one within the context of that process while ETS will be a synchronized access to shared data.
rvirding