[erlang-questions] Parsing binaries performance

Bjorn Gustavsson <>
Thu Jun 26 10:21:52 CEST 2008

Darren New <> writes:

> Another possibility would be to try code that simply indexes into the 
> binary instead of breaking it apart into a new binary, as that might not 
> need to copy things around as much.  Something like
> check_pos(Bin, Inx, Chr) ->
>    <<_:Inx/binary, MaybeCh:integer, _/binary>>,
>    Chr == MaybeCh.
> find_cr(Bin) -> find_cr(Bin, 0).
> find_cr(Bin, Inx) when Inx >= size(Bin) -> false;
> find_cr(Bin, Inx) -> check_pos(Bin, Inx, $\r)
>    andalso check_pos(Bin, Inx+1, $\n).

That kind of optimization was good before R12B, but in R12B
it will generally be slower. See the Efficiency Guide:


Also, binary matching in R12B never copies any binaries.
Before R12B, unaligned binaries would be copied, but never
binaries starting on a byte boundary.

Björn Gustavsson, Erlang/OTP, Ericsson AB

More information about the erlang-questions mailing list