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 self-assigned this Sep 3, 2018
@bjorng bjorng merged commit 66b797b into erlang:master Sep 5, 2018
2 checks passed
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/opt-put-tuple branch Jan 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

1 participant
You can’t perform that action at this time.