[erlang-questions] binary_part/3

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Mon Jan 8 20:20:18 CET 2018

```A sub-binary is a pointer into an already existing binary. It is, roughly,
a triple of a pointer to the original binary, an offset into the binary,
and a length (from that offset). They are quick to copy around, and they
are making efficient use of the fact that data in an Erlang system is
generally immutable.

However, the caveat is that a sub-binary can keep an underlying binary
"alive" for longer than it should. There are situations in which the ERTS
will convert a sub-binary into a copy with a new binary, and the situation
is slowly getting better and better. But if you want to be sure, you need
to add a binary:copy/1 to a binary to make it into a full-blown
(ref-counted) binary.

In the example Alex writes, the f(A) in (3) merely forgets the ref-counted
binary pointer. But the binary in (2) and (4) keeps it alive in the system.

On Mon, Jan 8, 2018 at 8:09 PM Frank Muller <frank.muller.erl@REDACTED>
wrote:

> OTP team,
>
> Anyone can shed some light on this please?
>
> /Frank
>
> Okay, maybe I'm totally confused here, but is it a pointer to the original
>> binary or a new binary?  Looking at the source code it seems to build/make
>> a new binary at the end of erts_gc_binary_part().  And from the shell...
>>
>> 1> A = <<"A cookie is yummy!">>.
>> 2> B = erlang:binary_part(A, 0, 8).
>> 3> f(A).
>> ok
>> 4> B.
>>
>> A is gone and we still have B.  Does Erlang actually returns references?
>>
>> Cheers,
>> Alex
>>
>> On 1/7/2018 at 1:21 PM, "Frank Muller" <frank.muller.erl@REDACTED>
>> wrote:
>>
>> Thank you Roger!!!
>>
>> It's a sub-binary:
>>>
>>>
>>> https://github.com/erlang/otp/blob/master/erts/emulator/beam/erl_bif_binary.c#L1915-L1924
>>>
>>> On 6 January 2018 at 21:38, Frank Muller <frank.muller.erl@REDACTED>
>>> wrote:
>>> > Hi Everyone
>>> >
>>> > Is the binary returned when calling binary_part/3:
>>> > 1.  a sub-binary pointing to the original one
>>> > 2. or a new binary
>>> >
>>> > Thank you.
>>> > Frank
>>> >
>>> > _______________________________________________
>>> > erlang-questions mailing list
>>> > erlang-questions@REDACTED
>>> > http://erlang.org/mailman/listinfo/erlang-questions
>>> >
>>>
>> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180108/82306d6c/attachment.htm>
```