Skip to content

Commit 16fb815

Browse files
authored
Optimize for comprehensions into: MapSet (#14796)
1 parent 3146daf commit 16fb815

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

lib/elixir/src/elixir_erl_for.erl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
-export([translate/3]).
77
-include("elixir.hrl").
88

9+
-define(empty_map_set_pattern, {map, _, [
10+
{map_field_assoc, _, {atom, _, '__struct__'}, {atom, _, 'Elixir.MapSet'}},
11+
{map_field_assoc, _, {atom, _, map}, {map, _, []}}
12+
]}).
13+
914
translate(Meta, Args, S) ->
1015
{Cases, [{do, Expr} | Opts]} = elixir_utils:split_last(Args),
1116

@@ -156,6 +161,10 @@ build_inline_each(Ann, Clauses, Expr, {bin, _, []}, Uniq, S) ->
156161
build_into(Ann, Clauses, Expr, {map, _, []}, Uniq, S) ->
157162
{ReduceExpr, SR} = build_inline_each(Ann, Clauses, Expr, {nil, Ann}, Uniq, S),
158163
{?remote(Ann, maps, from_list, [ReduceExpr]), SR};
164+
build_into(Ann, Clauses, Expr, ?empty_map_set_pattern = _Into, Uniq, S) ->
165+
InnerFun = fun(InnerExpr, InnerAcc) -> {cons, Ann, InnerExpr, InnerAcc} end,
166+
{ReduceExpr, SR} = build_reduce(Ann, Clauses, InnerFun, Expr, {nil, Ann}, Uniq, S),
167+
{?remote(Ann, 'Elixir.MapSet', new, [ReduceExpr]), SR};
159168
build_into(Ann, Clauses, Expr, Into, Uniq, S) ->
160169
{Fun, SF} = build_var(Ann, S),
161170
{Acc, SA} = build_var(Ann, SF),

0 commit comments

Comments
 (0)