<div>Hi Joe,</div><div><br></div><div>I also have many term_to_binary calls and as a result many-many binary data chunks stored in DB.</div><div><br></div><div>As I understood to use your technique I need pimped versions of hd, tl and element functions. Can you please provide a little bit more details about it plase?</div>

<br clear="all"><div>Best regards,</div><div>Max</div><br><br>
<br><br><div class="gmail_quote">On Thu, Nov 17, 2011 at 5:52 PM, Joe Armstrong <span dir="ltr"><<a href="mailto:erlang@gmail.com">erlang@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div>Here's a programing technique that might be useful which I haven't seen</div><div>described before ...</div><div><br></div><div>I've playing with unpacking binaries produced by term_to_binary(Term) in</div>


<div>other languages. Specifically I do term_to_binary in Erlang creating binary and I send the</div><div>binary to javascript. The javascript code does not by default decode the entire binary, </div><div>but accesses sub-terms through selector functions (you only need element, hd and tl)</div>


<div><br></div><div>This technique seems much nicer than mucking around with JSON </div><div>binary formats are way easier to manipulate than than text formats that need parsing.</div><div><br></div><div>Now of course you can do the same thing in Erlang, you do not have to</div>


<div>do binary_to_term(B) to extract a sub-term, but can traverse the internal structure</div><div>of the external format and pull out exactly what you want and nothing else.</div><div><br></div><div>I often store large terms in files and databases using term_to_binary</div>


<div>and I extract data by first doing binary_to_term and then pattern matching on the result.</div><div><br></div><div>For example if I create a binary with:</div><div><br></div><div>   > B = term_to_binary({foo,bar,[a,b]})</div>


<div><br></div><div>And I want to extract the 'b' sub term, I'd normally write</div><div><br></div><div>     {_, _, [_,X]} = binary_to_term(B)</div><div><br></div><div>But why bother to unpack? I could just as well write</div>


<div><br></div><div>     X = hd(tl(element(3,B))) </div><div><br></div><div>This is not the regular hd/tl/and element but a hacked version that can traverse the external format.</div><div><br></div><div>If the term inside the external format is large and if I only want to extract a few parameters </div>


<div>then this method should be lot faster than actually building a large term, just to throw it away after pattern matching.</div><div>This should be a  GC and cache friendly way of doing things.</div><div><br></div><div>


In a similar vein one could think of pattern matching being extended over packed terms.</div><div><br></div><div>If this were so I could write:</div><div><br></div><div>     T = {foo,bac,[a,b]}</div><div>     B = term_to_binary(T),</div>


<div>     match(B).</div><div><br></div><div>match({_,_,[_,X]}) -> X</div><div><br></div><div>Doing so would mean that once we have packed terms using term_to_binary we could leave them</div><div>alone and extract data from them without having to completely unpack them.</div>


<div><br></div><div>This should be very cache friendly - Erlang terms can be scatter all over the place in virtual memory</div><div>but in the external form all the term is kept together in memory</div><div><br></div><div>


This is actually pretty useful - I have a data structure representing a book - somewhere near the beginning there is a title</div><div>the entire book is stored on disk as a term_to_binary encoded blob. Now I have a large numbers of these</div>


<div>representing ebooks. If I want to list all titles I certainly do not want to complete unpack everything, </div><div>I only want to extract the title field and nothing else. ...</div><div><br></div><div>Cheers</div><span class="HOEnZb"><font color="#888888"><div>


<br></div><div>/Joe</div><div><br></div><div><br></div>
</font></span><br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br>