[erlang-questions] *current* value?
Paulo Sérgio Almeida
psa@REDACTED
Thu Oct 18 00:42:08 CEST 2007
Hi,
YC wrote:
> Hopefully this follow-up doesn't make me sound like a troll - but I
> can't help but notice that ets and even Mnesia is recommended before
> process dictionary (which is very heavily frowned upon) in this thread,
> and I am not sure if I understand the reason, unless process dictionary
> is technically inferior or broken. From FP perspective, neither is
> pure, so why one better than another?
>
> There must be a reason for the level of discouragement about a built-in
> facility.
I think people are exagerating a bit on the discouragement. There are
bad examples, like using it in general purpose libraries or doing
intermodule sets and gets. But using it in a small module that contains
the main loop of the process can result in something readable. This is a
way to simulate objects, with "instance variables" being in the process
dictionary. But philosophy apart, the sensible thing to do is:
- for small state, pass it around in a parameter in the process loop,
e.g. in a record;
- for a large number of *SMALL* entries, or when advanced lookup (like
select) is needed, use ets;
- for a large state with lots of substructure sharing (e.g. a forest
with lots of common large sub-trees), use the process dictionary. An
important difference from ets is that no copying is done: we are only
referencing process memory, while with ets terms are copied to a
separate memory area and no subterm sharing is preserved, resulting in
possibly much larger memory requirements.
Regards,
Paulo
More information about the erlang-questions
mailing list