4. Constructing and Matching Binaries

4 Constructing and Matching Binaries

In R12B, the most natural way to construct and match binaries is significantly faster than in earlier releases.

To construct a binary, you can simply write as follows:

DO (in R12B) / REALLY DO NOT (in earlier releases)

my_list_to_binary(List) ->
    my_list_to_binary(List, <<>>).

my_list_to_binary([H|T], Acc) ->
    my_list_to_binary(T, <<Acc/binary,H>>);
my_list_to_binary([], Acc) ->
    Acc.

In releases before R12B, Acc is copied in every iteration. In R12B, Acc is copied only in the first iteration and extra space is allocated at the end of the copied binary. In the next iteration, H is written into the extra space. When the extra space runs out, the binary is reallocated with more extra space. The extra space allocated (