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

Add fast-lanes for '++'/2 and '--'/2 on nil arguments #8743

Merged
merged 1 commit into from
Sep 10, 2024

Conversation

the-mikedavis
Copy link
Contributor

Currently appending an empty list to another list causes the left-hand side list to be fully copied. We can skip the append operation when the right-hand side is nil since it shouldn't change the left-hand side list.

It looks like there are historical artifacts in the ++/2 implementation like its behavior on [] ++ anything so I'm not sure if I'm missing some context, but I wanted to propose this optimization since it seems needless to allocate for List ++ []. What do you think?

Copy link
Contributor

github-actions bot commented Aug 24, 2024

CT Test Results

    3 files    141 suites   49m 20s ⏱️
1 586 tests 1 536 ✅ 49 💤 1 ❌
2 287 runs  2 217 ✅ 69 💤 1 ❌

For more details on these failures, see this check.

Results for commit 8eeb95d.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

erts/emulator/beam/erl_bif_lists.c Outdated Show resolved Hide resolved
lib/stdlib/test/lists_SUITE.erl Outdated Show resolved Hide resolved
@rickard-green rickard-green added the team:VM Assigned to OTP team VM label Aug 26, 2024
@jhogberg jhogberg self-assigned this Aug 26, 2024
@the-mikedavis the-mikedavis changed the title Skip appending in List++[] Add fast-lanes for '++'/2 and '--'/2 on nil arguments Aug 26, 2024
erts/emulator/beam/erl_bif_lists.c Outdated Show resolved Hide resolved
erts/emulator/beam/erl_bif_lists.c Outdated Show resolved Hide resolved
@the-mikedavis the-mikedavis force-pushed the md/optimize-plus-plus-nil-rhs branch 3 times, most recently from 601905b to d9db77d Compare August 26, 2024 22:03
erts/emulator/beam/erl_bif_lists.c Outdated Show resolved Hide resolved
erts/emulator/beam/erl_bif_lists.c Outdated Show resolved Hide resolved
erts/emulator/beam/erl_bif_lists.c Show resolved Hide resolved
erts/emulator/beam/erl_bif_lists.c Outdated Show resolved Hide resolved
@the-mikedavis the-mikedavis force-pushed the md/optimize-plus-plus-nil-rhs branch 2 times, most recently from 0d371fc to 1c429ad Compare August 27, 2024 18:08
Comment on lines 238 to 250
int res;

/* First check that lhs is proper. */
res = append_is_proper_list(p, &context->iterator);

if (res == 1) {
#ifdef DEBUG
context->result_cdr = &context->rhs_original;
#endif
context->result = context->lhs_original;
}

return res;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move setting these to the branch returning 1 in append_is_proper_list, we can shorten this a lot. While we're at it we can change its name to append_empty_rhs or the likes.

Otherwise this PR LGTM.

Currently appending an empty list to another list causes the left-hand
side list to be fully copied. We can skip the append operation when the
right-hand side is nil since it shouldn't change the left-hand side
list. In this fast-lane we scan the list to determine if it is improper
so that cases like `[1, 2 | 3] ++ []` behave the same as cases like
`[1, 2 | 3] ++ [4]`: return a badarg error.

This patch adds similar fast-lanes for '--'/2: we can return the first
argument when either the first or second arguments are nil.
@jhogberg jhogberg added the testing currently being tested, tag is used by OTP internal CI label Sep 9, 2024
@jhogberg jhogberg merged commit 57eae43 into erlang:master Sep 10, 2024
15 of 17 checks passed
@jhogberg
Copy link
Contributor

Merged, thanks for the PR!

@the-mikedavis the-mikedavis deleted the md/optimize-plus-plus-nil-rhs branch September 10, 2024 12:45
@the-mikedavis
Copy link
Contributor Author

Thanks for your help with this @jhogberg! I wasn't familiar with the internals and trapping so this was a cool learning experience :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

4 participants