[erlang-questions] Data in state records

Robert Raschke <>
Tue Mar 13 07:42:34 CET 2012


On Mon, Mar 12, 2012 at 5:05 PM, Matthew Evans <>wrote:

>  Hi,
>
> Many of my processes (gen_servers) need to store some kind of indexed data
> in the state record. Usually this involves creating a set/dict/ets table in
> the init/1 function, assigning it to an element in the state record and
> using the data on each handle_call/cast/info.
>
> My usual strategy is anything that is 100% private to my gen_server and
> will contain less than 50 or so records I will use sets/gb_trees or
> similar. In all other cases I would use an ETS table. Is there an
> official recommendation?
>
> Thanks
>
> Matt
>
>
It depends a lot on your usage patterns. But for anything less than 50 to
100, I tend to simply use a proplist or keylist, as they "print" nicely in
debugging situations. A set is good for when you need a set. The other data
structures have characteristics that map well onto certain types of usage
patterns. But my code hasn't so far needed anything more complicated. I
view ets as a good stepping point to dets, if I know that I may want to
persist stuff, I might opt for ets first. But even that has turned out to
be premature optimisation.

It might be nice to have an overview summary of the various standard data
structures with examples of when each of them is most appropriate.

But in summary, I'd recommend sticking with the simplest data structure,
and only increase complexity if you find that you need it. I'm one of those
people who have no fear of rewriting code over and over, though. So YMMV.

Robby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120313/6b8ff835/attachment.html>


More information about the erlang-questions mailing list