質問

After reading this document, I'm not sure whether or not the following code will make the run-time copy binaries of A internally.

f(<<>>, A) ->
    A;
f(<<X:2/binary, R/binary>>, A) ->
    I = binary_to_integer(X, 16),
    f(R, <<A/binary, I>>).

My guess is "no", because A isn't sent nor is it deconstructed. Am I right, or did I miss something?

役に立ちましたか?

解決

Your code will not result in run-time copying of the A binaries since no sharing of sub-binaries is done. If we manually unroll the sequence of append operations it looks something like

A0 = <<A/binary, SomeByte>>,
A1 = <<A0/binary, SomeByte1>>,
A2 = <<A1/binary, SomeByte2>>,
 .....
An = <<An-1, SomeByteN>>.

So we are only appending to the binary resulting from the latest append operation, i.e. there is a single reference to the ProcBin that was created for A0(as described in the efficiency documentation).

他のヒント

Why not use bin_opt_info option ?

For your code :

[root@nimbus bin_test]# erlc +bin_opt_info a.erl
a.erl:8: Warning: OPTIMIZED: creation of sub binary delayed

For the code in https://gist.github.com/wardbekker/5673200

The output is :

[root@nimbus bin_test]# erlc +bin_opt_info test.erl 
test.erl:14: Warning: OPTIMIZED: creation of sub binary delayed
[root@nimbus bin_test]# erlc +bin_opt_info test2.erl
test2.erl:8: Warning: variable 'A' is unused
test2.erl:13: Warning: OPTIMIZED: creation of sub binary delayed
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top