[erlang-questions] A couple of questions about constructing and matching binaries

Sergey S ad.sergey@REDACTED
Mon Jan 26 23:47:52 CET 2009


The following questions arose while I was reading Efficiency Guide / 4
Constructing and matching binaries:

#1 Please, take a look at the following quote (from 4.2 Constructing binaries):

Bin0 = <<0>>,                           %% 1
Bin1 = <<Bin0/binary,1,2,3>>,    %% 2
                                                %% Lines 3 to 6 have
been skippped

"The first line (marked with the %% 1 comment), assigns a heap binary
to the variable Bin0.

The second line is an append operation. Since Bin0 has not been
involved in an append operation, >>> a new refc binary will be created
<<< and the contents of Bin0 will be copied into it."

Why has Bin1 become reference-counted binary, not the heap one? It's
still too small (smaller than 64 bytes) to be stored outside of the

I don't understand why "Since Bin0 has not been involved in an append
operation" is the reason of moving the binary to the storage of refc
binaries :/

#2 Here is another quote, which is also not so clear to me:

The optimization of the binary append operation requires that there is
a single ProcBin and a single reference to the ProcBin for the binary.

What does "reference to the ProcBin" mean?

For example we have code like this:

Bin1 = Bin2 = Bin3 = list_to_binary(lists:duplicate(128, $A))

Has that code created three ProcBins (Bin1, Bin2 and Bin3) or there is
only one ProcBin and three references to it?



More information about the erlang-questions mailing list