Last active
December 17, 2015 21:10
-
-
Save wardbekker/5673200 to your computer and use it in GitHub Desktop.
Used in http://stackoverflow.com/questions/16815317/erlang-binary-will-this-result-in-a-copy call with spawn(test, bla, []).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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>>). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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>>). |
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
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>