<span style="font-family: Arial; font-size: 13px;">Although I was not the original author of this thread, thanks Jesper for clearing this and the background info!<br><br>Cheers,<br>Alex<br><br>On 1/8/2018 at 2:20 PM, "Jesper Louis Andersen" <jesper.louis.andersen@gmail.com> wrote:<blockquote style="border-left:solid 1px #ccc;margin-left:10px;padding-left:10px;"><div dir="ltr">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.<div><br></div><div>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.</div><div><br></div><div>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.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Jan 8, 2018 at 8:09 PM Frank Muller <<a>frank.muller.erl@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="border-left:1px #ccc solid;padding-left:1ex;margin:0 0 0 .8ex;"><div><div class="gmail_quote"><div>OTP team,</div><div dir="auto"><br></div><div dir="auto">Anyone can shed some light on this please?</div></div></div><div><div class="gmail_quote"><div dir="auto"><br></div><div dir="auto">/Frank</div></div></div><div><div class="gmail_quote"><div dir="auto"><br></div><blockquote class="gmail_quote" style="border-left:1px #ccc solid;padding-left:1ex;margin:0 0 0 .8ex;"><span style="font-family:Arial;font-size:13px;">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 <span class="m_7057228071007366601m_2417699851828048986pl-en">erts_gc_binary_part().  And from the shell...<br><br>1> A = <<"A cookie is yummy!">>.<br><<"A cookie is yummy!">><br>2> B = erlang:binary_part(A, 0, 8).<br><<"A cookie">><br></span>3> f(A).<br>ok<br>4> B.<br><<"A cookie">><br><br>A is gone and we still have B.  Does Erlang actually returns references?<br><br>Cheers,<br>Alex</span><span style="font-family:Arial;font-size:13px;"><br><br>On 1/7/2018 at 1:21 PM, "Frank Muller" <<a>frank.muller.erl@gmail.com</a>> wrote:<blockquote style="border-left:solid 1px #ccc;margin-left:10px;padding-left:10px;"><div><div class="gmail_quote"><div>Thank you Roger!!!</div><div dir="auto"><br></div><blockquote class="gmail_quote" style="border-left:1px #ccc solid;padding-left:1ex;margin:0 0 0 .8ex;">It's a sub-binary:<br>
<br>
<a target="_blank" href="https://github.com/erlang/otp/blob/master/erts/emulator/beam/erl_bif_binary.c#L1915-L1924" onclick="window.open('https://github.com/erlang/otp/blob/master/erts/emulator/beam/erl_bif_binary.c#L1915-L1924');return false;">https://github.com/erlang/otp/blob/master/erts/emulator/beam/erl_bif_binary.c#L1915-L1924</a><br>
<br>
On 6 January 2018 at 21:38, Frank Muller <<a>frank.muller.erl@gmail.com</a>> wrote:<br>
> Hi Everyone<br>
><br>
> Is the binary returned when calling binary_part/3:<br>
> 1.  a sub-binary pointing to the original one<br>
> 2. or a new binary<br>
><br>
> Thank you.<br>
> Frank<br>
><br>
> _______________________________________________<br>
> erlang-questions mailing list<br>
> <a>erlang-questions@erlang.org</a><br>
> <a target="_blank" href="http://erlang.org/mailman/listinfo/erlang-questions" onclick="window.open('http://erlang.org/mailman/listinfo/erlang-questions');return false;">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
><br>
</blockquote></div></div></blockquote></span></blockquote></div></div>
_______________________________________________<br>
erlang-questions mailing list<br>
<a>erlang-questions@erlang.org</a><br>
<a target="_blank" href="http://erlang.org/mailman/listinfo/erlang-questions" onclick="window.open('http://erlang.org/mailman/listinfo/erlang-questions');return false;">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div></blockquote></span>