binary_part/3

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

binary_part/3

Frank Muller
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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: binary_part/3

Roger Lipscombe-2
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 <[hidden email]> 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
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: binary_part/3

Frank Muller
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 <[hidden email]> 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
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: binary_part/3

Alex Alvarez-4
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!">>.
<<"A cookie is yummy!">>
2> B = erlang:binary_part(A, 0, 8).
<<"A cookie">>
3> f(A).
ok
4> B.
<<"A cookie">>

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" <[hidden email]> wrote:
Thank you Roger!!!

It's a sub-binary:

<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

On 6 January 2018 at 21:38, Frank Muller <[hidden email]> 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
> [hidden email]
> <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
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: binary_part/3

Frank Muller
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!">>.
<<"A cookie is yummy!">>
2> B = erlang:binary_part(A, 0, 8).
<<"A cookie">>
3> f(A).
ok
4> B.
<<"A cookie">>

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" <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: binary_part/3

Jesper Louis Andersen-2
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 <[hidden email]> 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!">>.
<<"A cookie is yummy!">>
2> B = erlang:binary_part(A, 0, 8).
<<"A cookie">>
3> f(A).
ok
4> B.
<<"A cookie">>

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" <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: binary_part/3

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

Cheers,
Alex

On 1/8/2018 at 2:20 PM, "Jesper Louis Andersen" <[hidden email]> 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 <[hidden email]> 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!">>.
<<"A cookie is yummy!">>
2> B = erlang:binary_part(A, 0, 8).
<<"A cookie">>
3> f(A).
ok
4> B.
<<"A cookie">>

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" <[hidden email]> wrote:
Thank you Roger!!!

It's a sub-binary:

<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

On 6 January 2018 at 21:38, Frank Muller <[hidden email]> 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
> [hidden email]
> <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
>
_______________________________________________
erlang-questions mailing list
[hidden email]
<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

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions