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

Explicitly add list pattern when matching on non-empty list on the Erlang target #4215

Open
giacomocavalieri opened this issue Feb 4, 2025 · 1 comment
Labels
help wanted Contributions encouraged priority:low

Comments

@giacomocavalieri
Copy link
Member

From Erlang's efficiency guide:

Don't:

map_pairs1(_Map, [], Ys) -> Ys;
map_pairs1(_Map, Xs, []) -> Xs;
map_pairs1(Map, [X|Xs], [Y|Ys]) -> todo.

Do:

map_pairs2(_Map, [], Ys) -> Ys;
map_pairs2(_Map, [_|_]=Xs, [] ) -> Xs;
map_pairs2(Map, [X|Xs], [Y|Ys]) -> todo.

In the second example, since Xs can no longer match with anything, the compiler is free to reorder the branches and make it as efficient as possible. I wonder if the Gleam compiler could emit code that plays nicely with this when it can tell you're matching on a non-empty list.

@lpil
Copy link
Member

lpil commented Feb 5, 2025

Sounds good! As a small perf optimisation let's not do this right now unless it can be implemented very easily and quickly. Or if anyone suuuper wants to do it 😁

@lpil lpil added help wanted Contributions encouraged priority:low labels Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Contributions encouraged priority:low
Projects
None yet
Development

No branches or pull requests

2 participants