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

Bjorn Gustavsson bgustavsson@REDACTED
Tue Jan 27 15:06:44 CET 2009


On Mon, Jan 26, 2009 at 11:47 PM, Sergey S <ad.sergey@REDACTED> wrote:
> Hello.
>
> 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):
>
> -----------------8<---------------
> 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."
> ----------------->8---------------
>
> 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
> heap.

Bin1 has been involved in an append operation. (It was created by appending
to a copy of the contents of Bin0.) The run-time system optimistically assumes
that there will be more appends to the same binary, so it allocates it as
a reference-counted binary. (It is not possible to append to heap binaries.)

> 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 :/

The CONTENTS of Bin0 has been involved in an append operation,
but not the binary itself. Bin0 is unchanged.

>
> #2 Here is another quote, which is also not so clear to me:
>
> -----------------8<---------------
> The optimization of the binary append operation requires that there is
> a single ProcBin and a single reference to the ProcBin for the binary.
> ----------------->8---------------
>
> What does "reference to the ProcBin" mean?
>

It is means a reference from a sub binary. (I agree that this is not clear from
context and not mentioned.)

> 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?
>

There is one ProcBin, one sub binary, and three references to the sub binary.

I might do some revisions to the text in the R13 release. I am not sure how much
I should try to explain. The text could get quite long if I tried to
explain all details
and tricks done under the hood.

/Bjorn
-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list