[erlang-questions] data sharing is outside the semantics of Erlang, but it sure is useful

Bjorn Gustavsson bgustavsson@REDACTED
Thu Sep 17 12:28:10 CEST 2009


On Mon, Sep 14, 2009 at 6:22 PM, James Hague <james.hague@REDACTED> wrote:
> This is easy to do in Erlang, because the compiler is very literal:
>
>   fix_tuple({H, H}) -> {H, H};
>   ...
>
> That ensures that identical looking elements in the tuple are sharing
> memory locations.  But there is absolutely no reason the compiler has
> to do this.  It would be perfectly valid to optimize away the entire
> function, just returning the original value.

We have no intention of introducing "optimizations" that would break
that code.

> Would any existing standard library functions make this nicer?  What I
> really want is to have a gb_trees:insert function that returns
> {NewTree, InsertedValue} where InsertedValue references existing data
> (unless it wasn't already in the tree; in that case, InsertedValue is
> exactly what I passed in).  Then I can happily use InsertedValue,
> knowing data is being shared.

In Wings3D (which is an application that depends heavily on sharing),
I use a gb_tree to keep track of values that I want to share.
I enter each term as both the key and the value and then I just do
gb_trees:get/2
to retrieve the shared value.

/Bjorn

-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list