[erlang-questions] term_to_binary and record improvements

Joe Armstrong <>
Fri Aug 29 12:07:03 CEST 2008


2008/8/29 Bjorn Gustavsson <>:
> On Fri, Aug 29, 2008 at 10:03 AM, Ulf Wiger (TN/EAB)
> <> wrote:
>>
>> BEAM already has the ability to do sharing-preserving copy:
>> the garbage collector obviously does it. But the feature is
>> not available to the lowly Erlang programmer.
>
> It is easy to preserve sharing if you are allowed to destroy the source term
> (as in the garbage collector).
>
> Preserving sharing when copying a term (i.e. not destroying the source term)
> is harder and
> WILL be slower compared to not preserve sharing.

So exactly why is it slower? - I assume you'd need two passes - and
have to toggle
some "this word has been moved" bit in the original term (is this
correct) on the
plus side the output term will be smaller so have better cache behavior.

Traversing a term twice should be no problem for a small term, since
it is small it will be fast -
but for a large term will take a long time, but here the cache
advantages could help.

Only the programmer knows if a term shares much data, so we could have
two bifs term_to_shared_binary and term_to_binary both of which can be unpacked
with binary_to_term. Both are declaratively correct, but the hint will
help performance.

Would this be a solution? - if so we could then implement records
with slightly more complex data structures.

ie #foo{name,age} might internally be the tuple
{{'foo-name-age',[name,age]}, Name, Age}

(or something in line with Richard's proposal
http://www.erlang.org/pipermail/erlang-questions/2005-September/017199.html

/Joe








>
> /Bjorn
>
> --
> Björn Gustavsson, Erlang/OTP, Ericsson AB
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list