Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys_core_bsm: Rearrange arguments to enable delayed sub binary creation #1687

Merged

Conversation

bjorng
Copy link
Contributor

@bjorng bjorng commented Jan 17, 2018

Argument order can prevent the delayed sub binary creation.
Here is an example directly from the Efficiency Guide:

non_opt_eq([H|T1], <<H,T2/binary>>) ->
    non_opt_eq(T1, T2);
non_opt_eq([_|_], <<_,_/binary>>) ->
    false;
non_opt_eq([], <<>>) ->
    true.

When compiling with the bin_opt_info option, there will be a
suggestion to change the argument order.

It turns out sys_core_bsm can itself change the order, not the
order of the arguments of themselves, but the order in which
the arguments are matched. Here is how it can be rewritten in
pseudo Core Erlang code:

non_opt_eq(Arg1, Arg2) ->
    case < Arg2,Arg1 > of
         < <<H1,T2/binary>>, [H2|T1] > when H1 =:= H2 ->
             non_opt_eq(T1, T2);
         < <<_,T2/binary>ffff>, [_|T1] > ->
             false;
         < <<>>, [] >> ->
             true
    end.

When rewritten like this, the bs_start_match2 instruction will be
the first instruction in the function and it will be possible to
store the match context in the same register as the binary
({x,1} in this case) and to delay the creation of sub binaries.

The switching of matching order also enables many other simplifications
in sys_core_bsm, since there is no longer any need to pass the position
of the pattern as an argument.

We will update the Efficiency Guide in a separate branch before the
release of OTP 21.

Argument order can prevent the delayed sub binary creation.
Here is an example directly from the Efficiency Guide:

    non_opt_eq([H|T1], <<H,T2/binary>>) ->
        non_opt_eq(T1, T2);
    non_opt_eq([_|_], <<_,_/binary>>) ->
        false;
    non_opt_eq([], <<>>) ->
        true.

When compiling with the bin_opt_info option, there will be a
suggestion to change the argument order.

It turns out sys_core_bsm can itself change the order, not the
order of the arguments of themselves, but the order in which
the arguments are matched. Here is how it can be rewritten in
pseudo Core Erlang code:

    non_opt_eq(Arg1, Arg2) ->
        case < Arg2,Arg1 > of
             < <<H1,T2/binary>>, [H2|T1] > when H1 =:= H2 ->
                 non_opt_eq(T1, T2);
             < <<_,T2/binary>ffff>, [_|T1] > ->
                 false;
             < <<>>, [] >> ->
                 true
        end.

When rewritten like this, the bs_start_match2 instruction will be
the first instruction in the function and it will be possible to
store the match context in the same register as the binary
({x,1} in this case) and to delay the creation of sub binaries.

The switching of matching order also enables many other simplifications
in sys_core_bsm, since there is no longer any need to pass the position
of the pattern as an argument.

We will update the Efficiency Guide in a separate branch before the
release of OTP 21.
@bjorng bjorng added team:VM Assigned to OTP team VM enhancement testing currently being tested, tag is used by OTP internal CI labels Jan 17, 2018
@bjorng bjorng self-assigned this Jan 17, 2018
@bjorng bjorng requested a review from jhogberg January 17, 2018 12:11
@bjorng bjorng merged commit aa66b87 into erlang:master Jan 19, 2018
@bjorng bjorng deleted the bjorn/compiler/generalize-bsm/OTP-14774 branch January 19, 2018 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant