<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:Arial;}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:blue;
text-decoration:underline;}
span.Typed
{font-family:"Courier New";}
span.tty
{font-family:"Courier New";}
span.Name
{font-style:italic;}
span.Variable
{font-family:"Times New Roman";
font-style:italic;}
span.EmailStyle22
{font-family:Arial;
color:navy;}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
{page:Section1;}
-->
</style>
</head>
<body lang=EN-US link=blue vlink=blue>
<div class=Section1>
<p class=MsoNormal><span style='color:navy'>Follow-up question to the converse
problem: assembling binaries. If binaries are best handled like lists while reading
them, is it also true that binaries are best assembled back-to-front like
lists, putting the new binary chunk onto the beginning of the binary instead of
the end?</span></p>
<p class=MsoNormal><span style='color:navy'> </span></p>
<div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'>
<div>
<div class=MsoNormal align=center style='text-align:center'>
<hr size=2 width="100%" align=center tabindex=-1>
</div>
<p class=MsoNormal><b><span style='font-family:Tahoma'>From:</span></b><span
style='font-family:Tahoma'> erlang-questions-bounces@erlang.org
[mailto:erlang-questions-bounces@erlang.org] <b>On Behalf Of </b>Robert Virding<br>
<b>Sent:</b> Monday, November 24, 2008 13:58<br>
<b>To:</b> Vance Shipley; Robert Virding; Erlang Questions<br>
<b>Subject:</b> Re: [erlang-questions] Binary pattern matching and optimization</span></p>
</div>
<p class=MsoNormal> </p>
<p class=MsoNormal>2008/11/24 Vance Shipley <<a
href="mailto:vances@motivity.ca">vances@motivity.ca</a>></p>
<div>
<blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;
margin-left:4.8pt;margin-right:0in'>
<p class=MsoNormal>Robert,<br>
<br>
My thinking was that reading a big binary and then passing it<br>
with offsets to various parsing functions would be easier on<br>
the emulator than creating a new binary for each one. Is it<br>
the case that the compiler will optimize away the creation of<br>
these binaries?</p>
</blockquote>
<div>
<p class=MsoNormal style='margin-bottom:12.0pt'><br>
No, I don't think so. It optimises the following case:<br>
<br>
foo(<< ... , Rest/binary>>, ... ) -><br>
...<br>
bar(Rest, ...);<br>
<br>
The important thing is that the old reference to the binary is not used again
and can be reused by the compiler. If you have a big binary and want to pass
off chunks of it to different functions is a different problem. Then it is
probably better to pass the whole binary and an offset. However, doing
something like:<br>
<br>
foo(Offset, Bin, ...) -><br>
<< _:Offset/binary, Stuff, _/binary>> = Bin,<br>
...<br>
foo(Offset + N, Bin, ...)<br>
<br>
and skipping over the beginning each time is most likely not the best way to
step over bits of your chunk. I would do something like:<br>
<br>
foo(Offset, Bin, ...) -><br>
<< _:Offset/binary,TheGoodBit/binary >> = Bin,<br>
foo1(TheGoodBit, ...).<br>
<br>
and foo1 as my first example above which picks bit off the head of the bin.<br>
<br>
Robert</p>
</div>
</div>
</div>
</div>
</body>
</html>