[erlang-questions] Binary pattern matching and optimization

David Mercer dmercer@REDACTED
Mon Nov 24 23:04:26 CET 2008


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?

 

  _____  

From: erlang-questions-bounces@REDACTED
[mailto:erlang-questions-bounces@REDACTED] On Behalf Of Robert Virding
Sent: Monday, November 24, 2008 13:58
To: Vance Shipley; Robert Virding; Erlang Questions
Subject: Re: [erlang-questions] Binary pattern matching and optimization

 

2008/11/24 Vance Shipley <vances@REDACTED>

Robert,

My thinking was that reading a big binary and then passing it
with offsets to various parsing functions would be easier on
the emulator than creating a new binary for each one.  Is it
the case that the compiler will optimize away the creation of
these binaries?


No, I don't think so. It optimises the following case:

foo(<< ... , Rest/binary>>, ... ) ->
    ...
    bar(Rest, ...);

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:

foo(Offset, Bin, ...) ->
    << _:Offset/binary, Stuff, _/binary>> = Bin,
    ...
    foo(Offset + N, Bin, ...)

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:

foo(Offset, Bin, ...) ->
    << _:Offset/binary,TheGoodBit/binary >> = Bin,
    foo1(TheGoodBit, ...).

and foo1 as my first example above which picks bit off the head of the bin.

Robert

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081124/491c4964/attachment.htm>


More information about the erlang-questions mailing list