[erlang-questions] Binary pattern matching and optimization

Robert Virding rvirding@REDACTED
Mon Nov 24 20:57:52 CET 2008


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/3af99606/attachment.htm>


More information about the erlang-questions mailing list