[erlang-questions] Flag not documented but give speed up.

Никита Рощупкин roshhnikita@REDACTED
Tue Nov 17 11:55:21 CET 2015


Why don't we use the flag which is not documented and not used by default
when "+native" flag is enabled?

This is table comparison
https://drive.google.com/open?id=1P3nYD0EZXL16TqQ0WStqa3RqWt6riqd9MH25YsmnGmE

There is +strict_record_updates flag in Erlang which is used to disable
setelement for record updating. In this case get_tuple_element, put and
put_tuple are used instead of setelement. Unlike setelement, they create
new record with applied changes. For example, I wrote a simple method
(function) working with record, and translated it to a native code.

By the way, as we can see in this table, the +strict_record_updates flag
achieves greater speedup when used with +native flag. Likewise, the "put"
flag is better than "setelement" when used with +native.

-module(testing).
-compile(export_all).
-define(COUNT, 100000000).
-record(big_record,
    {
        id1 = 1,
        id2 = 2,
        id3 = 3,
        id4 = 4,
        id5 = 5,
        id6 = 6
    }).
big_record_1_update(_Config) ->
    State = #big_record{},
    call_big_record_1_update(?COUNT, State).
call_big_record_1_update(0, _) ->
    #big_record{id1 = 0};
call_big_record_1_update(Number_test, State) ->
    call_big_record_1_update(Number_test - 1, State#big_record{id1 =
Number_test}).


%%%%%%%%%%%%%%%%%%%%%%%%%%%


erl -S testing.erl
{function, all, 0, 2}.
  {label,1}.
    {line,[{location,"testing_06_SUITE.erl",5}]}.
    {func_info,{atom,testing_06_SUITE},{atom,all},0}.
  {label,2}.
    {move,{literal,[big_record_1_update,big_record_2_update,
                    big_record_3_update]},
          {x,0}}.
    return.

{function, big_record_1_update, 1, 4}.
  {label,3}.
    {line,[{location,"testing_06_SUITE.erl",18}]}.
    {func_info,{atom,testing_06_SUITE},{atom,big_record_1_update},1}.
  {label,4}.
    {move,{literal,{big_record,1,2,3,4,5,6}},{x,1}}.
    {move,{integer,100000000},{x,0}}.
    {call_only,2,{f,6}}.

{function, call_big_record_1_update, 2, 6}.
  {label,5}.
    {line,[{location,"testing_06_SUITE.erl",21}]}.
    {func_info,{atom,testing_06_SUITE},{atom,call_big_record_1_update},2}.
  {label,6}.
    {test,is_eq_exact,{f,7},[{x,0},{integer,0}]}.
    {move,{literal,{big_record,0,2,3,4,5,6}},{x,0}}.
    return.

 %% This is interesting moment
__________________________________________
 {label,7}.
    {allocate_zero,1,2}.
    {line,[{location,"testing_06_SUITE.erl",24}]}.
    {gc_bif,'-',{f,0},2,[{x,0},{integer,1}],{x,2}}.
    {test,is_tuple,{f,8},[{x,1}]}.
    {test,test_arity,{f,8},[{x,1},7]}.
    {get_tuple_element,{x,1},0,{x,3}}.
    {test,is_eq_exact,{f,8},[{x,3},{atom,big_record}]}.
    {move,{x,2},{y,0}}.
    {move,{x,0},{x,2}}.
    {move,{integer,2},{x,0}}.
    {line,[{location,"testing_06_SUITE.erl",24}]}.
    {call_ext,3,{extfunc,erlang,setelement,3}}.
    {move,{x,0},{x,1}}.
    {move,{y,0},{x,0}}.
    {call_last,2,{f,6},1}.
__________________________________________

{label,8}.
    {move,{literal,{badrecord,big_record}},{x,0}}.
    {line,[{location,"testing_06_SUITE.erl",24}]}.
    {call_ext,1,{extfunc,erlang,error,1}}.


%%%%%%%%%%%%%%%%%%%%%%%%%%%


erl -S +strict_record_updates testing.erl
{function, all, 0, 2}.
  {label,1}.
    {line,[{location,"testing_06_SUITE.erl",5}]}.
    {func_info,{atom,testing_06_SUITE},{atom,all},0}.
  {label,2}.
    {move,{literal,[big_record_1_update,big_record_2_update,
                    big_record_3_update]},
          {x,0}}.
    return.

{function, big_record_1_update, 1, 4}.
  {label,3}.
    {line,[{location,"testing_06_SUITE.erl",18}]}.
    {func_info,{atom,testing_06_SUITE},{atom,big_record_1_update},1}.
  {label,4}.
    {move,{literal,{big_record,1,2,3,4,5,6}},{x,1}}.
    {move,{integer,100000000},{x,0}}.
    {call_only,2,{f,6}}.

{function, call_big_record_1_update, 2, 6}.
  {label,5}.
    {line,[{location,"testing_06_SUITE.erl",21}]}.
    {func_info,{atom,testing_06_SUITE},{atom,call_big_record_1_update},2}.
  {label,6}.
    {test,is_eq_exact,{f,7},[{x,0},{integer,0}]}.
    {move,{literal,{big_record,0,2,3,4,5,6}},{x,0}}.
    return.

 %% This is interesting moment
__________________________________________
  {label,7}.
    {line,[{location,"testing_06_SUITE.erl",24}]}.
    {gc_bif,'-',{f,0},2,[{x,0},{integer,1}],{x,2}}.
    {test,is_tuple,{f,8},[{x,1}]}.
    {test,test_arity,{f,8},[{x,1},7]}.
    {get_tuple_element,{x,1},0,{x,3}}.
    {get_tuple_element,{x,1},2,{x,4}}.
    {get_tuple_element,{x,1},3,{x,5}}.
    {get_tuple_element,{x,1},4,{x,6}}.
    {get_tuple_element,{x,1},5,{x,7}}.
    {get_tuple_element,{x,1},6,{x,8}}.
    {test,is_eq_exact,{f,8},[{x,3},{atom,big_record}]}.
    {test_heap,8,9}.
    {put_tuple,7,{x,1}}.
    {put,{atom,big_record}}.
    {put,{x,0}}.
    {put,{x,4}}.
    {put,{x,5}}.
    {put,{x,6}}.
    {put,{x,7}}.
    {put,{x,8}}.
    {move,{x,2},{x,0}}.
    {call_only,2,{f,6}}.
__________________________________________

  {label,8}.
    {move,{literal,{badrecord,big_record}},{x,0}}.
    {line,[{location,"testing_06_SUITE.erl",24}]}.
    {call_ext,1,{extfunc,erlang,error,1}}.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20151117/702780be/attachment.htm>


More information about the erlang-questions mailing list