Skip to content

Instantly share code, notes, and snippets.

@wardbekker
Last active December 17, 2015 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wardbekker/5673200 to your computer and use it in GitHub Desktop.
Save wardbekker/5673200 to your computer and use it in GitHub Desktop.
-module(test).
-export([foo/2, bla/0]).
%% here we prepend and append, resulting in a copy
bla() ->
io:format("before: ~p~n", [erlang:process_info(self(),binary)]),
A = foo(<<"12345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578123457812345781234578">>, <<"append after me">>),
io:format("after: ~p~n", [erlang:process_info(self(),binary)]),
A.
foo(<<>>, A) ->
A;
foo(<<X:1/binary, R/binary>>, A) ->
I = binary_to_integer(X),
foo(R, <<I, A/binary, I>>).
-module(test).
-export([foo/2, bla/0]).
%% here we append, binary is not copied
bla() ->
io:format("before: ~p~n", [erlang:process_info(self(),binary)]),
A = foo(<<"1234578">>, <<"append after me">>),
io:format("after: ~p~n", [erlang:process_info(self(),binary)]).
foo(<<>>, A) ->
A;
foo(<<X:1/binary, R/binary>>, A) ->
I = binary_to_integer(X),
foo(R, <<A/binary, I>>).
@wardbekker
Copy link
Author

My output of copy.erl:

70> spawn(copy, bla, []).
before: {binary,[]}
after: {binary,[{25930408,256,1}]}
<0.23197.0>

My output of no_copy.erl:

76> spawn(no_copy, bla, []).
before: {binary,[]}
after: {binary,[{44537112,1801,1},
{25569608,1803,1},
{25571440,1805,1},
{25573272,1807,1}]}
<0.14673.5>

@ligaoren
Copy link

BinInfo is a list containing miscellaneous information about binaries currently being referred to by this process.
So, how can you figure out the code is optimized or not ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment