[erlang-questions] binary_part/3

Alex Alvarez alex@REDACTED
Mon Jan 8 21:04:08 CET 2018

Although I was not the original author of this thread, thanks Jesper
for clearing this and the background info!


On 1/8/2018 at 2:20 PM, "Jesper Louis Andersen"  wrote: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  wrote:
OTP team,
Anyone can shed some light on this please?
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 = .

2> B = erlang:binary_part(A, 0, 8).

3> f(A).
4> B.
A is gone and we still have B.  Does Erlang actually returns


On 1/7/2018 at 1:21 PM, "Frank Muller"  wrote:Thank you Roger!!!
It's a sub-binary:
 On 6 January 2018 at 21:38, Frank Muller  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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180108/4c4459cf/attachment.htm>

More information about the erlang-questions mailing list