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

Paul Mineiro <>
Tue Sep 15 23:34:58 CEST 2009


It still passes the test either way (ugh, my test sucks), but I think this
is a bugfix:

-- p

--- hashcons.erl.orig   2009-09-15 14:29:30.000000000 -0700
+++ hashcons.erl        2009-09-15 14:29:32.000000000 -0700
@@ -53,7 +53,7 @@
       Ref = dict:size (NewDict),
       { Ref,
         NewTerm,
-        dict:store (NewTerm, { Ref, NewTerm }, NewDict),
+        dict:store (Term, { Ref, NewTerm }, NewDict),
         dict:store (Ref, NewTerm, NewCodec)
       }
   end.


On Tue, 15 Sep 2009, Paul Mineiro wrote:

> i use erlang:term_to_binary to persist items in databases all the time, so
> this thread caught my attention.
>
> to stimulate discussion i pounded out some code that does hash consing
> (hashcons:share/1) and that does encoding and decoding suitable for
> composition with erlang:term_to_binary/2 (hashcons:encode/1) and
> erlang:binary_to_term/1 (hashcons:decode/1).  encoded terms don't
> necessarily have a smaller wire footprint even in a very favorable case
> (see below), but should decode into a shared data structure (as opposed to
> calling hashcons:share/1 after calling erlang:binary_to_term/1, which
> could create a temporary which is unacceptably huge).
>
> attached.
>
> -- p
>
> % erl
> Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0] [kernel-poll:false]
>
> Eshell V5.6.5  (abort with ^G)
> 1> hashcons:encode (lists:duplicate (10, wazzup)).
> {[0,0,0,0,0,0,0,0,0,0],[{0,wazzup}]}
> 2> size (erlang:term_to_binary (hashcons:encode (lists:duplicate (10, wazzup)), [ compressed ])).
> 35
> 3> size (erlang:term_to_binary (lists:duplicate (10, wazzup), [ compressed ])).
> 31
> 4> % not always smaller
> 4> size (erlang:term_to_binary (hashcons:encode (lists:duplicate (100, wazzup)), [ compressed ])).
> 38
> 5> size (erlang:term_to_binary (lists:duplicate (100, wazzup), [ compressed ])).
> 37
>



More information about the erlang-questions mailing list