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

Introduce a put_tuple2 instruction #1947

Merged
merged 1 commit into from
Sep 5, 2018
Merged

Conversation

bjorng
Copy link
Contributor

@bjorng bjorng commented Sep 3, 2018

Sometimes when building a tuple, there is no way to avoid an
extra move instruction. Consider this code:

make_tuple(A) -> {ok,A}.

The corresponding BEAM code looks like this:

{test_heap,3,1}.
{put_tuple,2,{x,1}}.
{put,{atom,ok}}.
{put,{x,0}}.
{move,{x,1},{x,0}}.
return.

To avoid overwriting the source register {x,0}, a move
instruction is necessary.

The problem doesn't exist when building a list:

%% build_list(A) -> [A].
{test_heap,2,1}.
{put_list,{x,0},nil,{x,0}}.
return.

Introduce a new put_tuple2 instruction that builds a tuple in a
single instruction, so that the move instruction can be eliminated:

%% make_tuple(A) -> {ok,A}.
{test_heap,3,1}.
{put_tuple2,{x,0},{list,[{atom,ok},{x,0}]}}.
return.

Note that the BEAM loader already combines put_tuple and put
instructions into an internal instruction similar to put_tuple2.
Therefore the introduction of the new instruction will not speed up
execution of tuple building itself, but it will be less work for
the loader to load the new instruction.

Sometimes when building a tuple, there is no way to avoid an
extra `move` instruction. Consider this code:

    make_tuple(A) -> {ok,A}.

The corresponding BEAM code looks like this:

    {test_heap,3,1}.
    {put_tuple,2,{x,1}}.
    {put,{atom,ok}}.
    {put,{x,0}}.
    {move,{x,1},{x,0}}.
    return.

To avoid overwriting the source register `{x,0}`, a `move`
instruction is necessary.

The problem doesn't exist when building a list:

    %% build_list(A) -> [A].
    {test_heap,2,1}.
    {put_list,{x,0},nil,{x,0}}.
    return.

Introduce a new `put_tuple2` instruction that builds a tuple in a
single instruction, so that the `move` instruction can be eliminated:

    %% make_tuple(A) -> {ok,A}.
    {test_heap,3,1}.
    {put_tuple2,{x,0},{list,[{atom,ok},{x,0}]}}.
    return.

Note that the BEAM loader already combines `put_tuple` and `put`
instructions into an internal instruction similar to `put_tuple2`.
Therefore the introduction of the new instruction will not speed up
execution of tuple building itself, but it will be less work for
the loader to load the new instruction.
@bjorng bjorng added team:VM Assigned to OTP team VM enhancement testing currently being tested, tag is used by OTP internal CI labels Sep 3, 2018
@bjorng bjorng self-assigned this Sep 3, 2018
@bjorng bjorng merged commit 66b797b into erlang:master Sep 5, 2018
@bjorng bjorng deleted the bjorn/opt-put-tuple branch January 16, 2019 17:35
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