[erlang-questions] Update on sharing constant data locally by reference rather than by copy?

Richard Carlsson richardc@REDACTED
Thu Mar 19 11:13:36 CET 2009


Olivier Boudeville wrote:
> would it be possible to have an update regarding how an immutable data
> structure (which is not a binary) could be shared between several
> processes on the same Erlang node, without having one copy of that data
> structure per process?
> 
> I know that the Erlang semantics allows processes to access that
> constant data as if there was only one copy of it, but, as I understand,
> in practice, currently, they do not access to that data through a const
> pointer but still rely on a private copy of it instead
> (http://www.erlang.org/pipermail/erlang-questions/2006-September/022739.html)

If the entire tables are constant over a long time, you could generate
them as modules. Nowadays, compile-time constants (even complex ones)
are placed in a constant pool associated with the module. So, you could
generate something like this:

  -module(autogen_table).
  -export([find/1]).
  find(0) -> {some_struct, "hello", ...};
  ...
  find(99) -> {some_other_struct, <<"hi!">>}
  find(X) -> throw({not_found, X}).

As far as I know, these constants will not be copied to the private
heaps of the processes. The generated code will also give you the
fastest possible lookup.

    /Richard



More information about the erlang-questions mailing list