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
merged 1 commit into from Jan 19, 2018

Conversation

Projects
None yet
1 participant
@bjorng
Contributor

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.

sys_core_bsm: Rearrange arguments to enable delayed sub binary creation
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 self-assigned this Jan 17, 2018

@bjorng bjorng requested a review from jhogberg Jan 17, 2018

@fenollp fenollp referenced this pull request Jan 18, 2018

Closed

Jan 19 #28

@bjorng bjorng merged commit aa66b87 into erlang:master Jan 19, 2018

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
license/cla Contributor License Agreement is signed.
Details

@bjorng bjorng deleted the bjorng:bjorn/compiler/generalize-bsm/OTP-14774 branch Jan 19, 2018

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