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

Ulf Wiger ulf.wiger@REDACTED
Thu Sep 17 18:06:04 CEST 2009

James Hague wrote:
> I've run into several cases where enforcing the sharing of data
> resulted in a significant memory savings.  

BTW, here is an example from OTP, written by Robert Virding,
no less, so it has to be an example of the type of code
that for which Erlang was originally intended. :)

%% expand_segs(Segs, EmptySeg) -> NewSegs.
%% contract_segs(Segs) -> NewSegs.
%%  Expand/contract the segment tuple by doubling/halving the number
%%  of segments.  We special case the powers of 2 upto 32, this should
%%  catch most case.  N.B. the last element in the segments tuple is
%%  an extra element containing a default empty segment.
expand_segs({B1}, Empty) ->
expand_segs({B1,B2}, Empty) ->
expand_segs({B1,B2,B3,B4}, Empty) ->
expand_segs({B1,B2,B3,B4,B5,B6,B7,B8}, Empty) ->
Empty) ->
expand_segs(Segs, Empty) ->
     ++ lists:duplicate(tuple_size(Segs), Empty)).

It is from dict.erl, and used to expand the hash table.

Very deliberate use of sharing.

Ulf W
Ulf Wiger
CTO, Erlang Training & Consulting Ltd

More information about the erlang-questions mailing list