From 9afb4256e00c9019df81a4a3c2f1cc0249d2c8b2 Mon Sep 17 00:00:00 2001 From: sabiwara Date: Mon, 29 Sep 2025 15:42:57 +0900 Subject: [PATCH 1/3] Optimize for comprehensions into: MapSet --- lib/elixir/src/elixir_erl_for.erl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/elixir/src/elixir_erl_for.erl b/lib/elixir/src/elixir_erl_for.erl index 81b88599da4..61e558df549 100644 --- a/lib/elixir/src/elixir_erl_for.erl +++ b/lib/elixir/src/elixir_erl_for.erl @@ -6,6 +6,11 @@ -export([translate/3]). -include("elixir.hrl"). +-define(empty_map_set_pattern, {map, _, [ + {map_field_assoc, _, {atom, _, '__struct__'}, {atom, _, 'Elixir.MapSet'}}, + {map_field_assoc, _, {atom, _, map}, {map, _, []}} + ]}). + translate(Meta, Args, S) -> {Cases, [{do, Expr} | Opts]} = elixir_utils:split_last(Args), @@ -156,6 +161,10 @@ build_inline_each(Ann, Clauses, Expr, {bin, _, []}, Uniq, S) -> build_into(Ann, Clauses, Expr, {map, _, []}, Uniq, S) -> {ReduceExpr, SR} = build_inline_each(Ann, Clauses, Expr, {nil, Ann}, Uniq, S), {?remote(Ann, maps, from_list, [ReduceExpr]), SR}; +build_into(Ann, Clauses, Expr, ?empty_map_set_pattern = _Into, Uniq, S) -> + InnerFun = fun(InnerExpr, InnerAcc) -> {cons, Ann, InnerExpr, InnerAcc} end, + {ReduceExpr, SR} = build_reduce(Ann, Clauses, InnerFun, Expr, {nil, Ann}, Uniq, S), + {?remote(Ann, 'Elixir.MapSet', new, [ReduceExpr]), SR}; build_into(Ann, Clauses, Expr, Into, Uniq, S) -> {Fun, SF} = build_var(Ann, S), {Acc, SA} = build_var(Ann, SF), From c713cde113427f0a266b872a8f4d6a365e1942db Mon Sep 17 00:00:00 2001 From: sabiwara Date: Mon, 29 Sep 2025 17:07:54 +0900 Subject: [PATCH 2/3] Even faster: build the map directly --- lib/elixir/src/elixir_erl_for.erl | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/elixir/src/elixir_erl_for.erl b/lib/elixir/src/elixir_erl_for.erl index 61e558df549..9bf598e2871 100644 --- a/lib/elixir/src/elixir_erl_for.erl +++ b/lib/elixir/src/elixir_erl_for.erl @@ -162,9 +162,16 @@ build_into(Ann, Clauses, Expr, {map, _, []}, Uniq, S) -> {ReduceExpr, SR} = build_inline_each(Ann, Clauses, Expr, {nil, Ann}, Uniq, S), {?remote(Ann, maps, from_list, [ReduceExpr]), SR}; build_into(Ann, Clauses, Expr, ?empty_map_set_pattern = _Into, Uniq, S) -> - InnerFun = fun(InnerExpr, InnerAcc) -> {cons, Ann, InnerExpr, InnerAcc} end, + InnerFun = fun(InnerExpr, InnerAcc) -> + % [{Expr, []} | Acc] + {cons, Ann, {tuple, Ann, [InnerExpr, {nil, Ann}]}, InnerAcc} + end, {ReduceExpr, SR} = build_reduce(Ann, Clauses, InnerFun, Expr, {nil, Ann}, Uniq, S), - {?remote(Ann, 'Elixir.MapSet', new, [ReduceExpr]), SR}; + MapSetExpr = {map, Ann, [ + {map_field_assoc, Ann, {atom, Ann, '__struct__'}, {atom, Ann, 'Elixir.MapSet'}}, + {map_field_assoc, Ann, {atom, Ann, map}, ?remote(Ann, maps, from_list, [ReduceExpr])} + ]}, + {MapSetExpr, SR}; build_into(Ann, Clauses, Expr, Into, Uniq, S) -> {Fun, SF} = build_var(Ann, S), {Acc, SA} = build_var(Ann, SF), From aeea3b5a7e8758caee85b2c0c10e1c8499b27014 Mon Sep 17 00:00:00 2001 From: sabiwara Date: Mon, 29 Sep 2025 18:14:59 +0900 Subject: [PATCH 3/3] Revert "Even faster: build the map directly" This reverts commit c713cde113427f0a266b872a8f4d6a365e1942db. --- lib/elixir/src/elixir_erl_for.erl | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/elixir/src/elixir_erl_for.erl b/lib/elixir/src/elixir_erl_for.erl index 9bf598e2871..61e558df549 100644 --- a/lib/elixir/src/elixir_erl_for.erl +++ b/lib/elixir/src/elixir_erl_for.erl @@ -162,16 +162,9 @@ build_into(Ann, Clauses, Expr, {map, _, []}, Uniq, S) -> {ReduceExpr, SR} = build_inline_each(Ann, Clauses, Expr, {nil, Ann}, Uniq, S), {?remote(Ann, maps, from_list, [ReduceExpr]), SR}; build_into(Ann, Clauses, Expr, ?empty_map_set_pattern = _Into, Uniq, S) -> - InnerFun = fun(InnerExpr, InnerAcc) -> - % [{Expr, []} | Acc] - {cons, Ann, {tuple, Ann, [InnerExpr, {nil, Ann}]}, InnerAcc} - end, + InnerFun = fun(InnerExpr, InnerAcc) -> {cons, Ann, InnerExpr, InnerAcc} end, {ReduceExpr, SR} = build_reduce(Ann, Clauses, InnerFun, Expr, {nil, Ann}, Uniq, S), - MapSetExpr = {map, Ann, [ - {map_field_assoc, Ann, {atom, Ann, '__struct__'}, {atom, Ann, 'Elixir.MapSet'}}, - {map_field_assoc, Ann, {atom, Ann, map}, ?remote(Ann, maps, from_list, [ReduceExpr])} - ]}, - {MapSetExpr, SR}; + {?remote(Ann, 'Elixir.MapSet', new, [ReduceExpr]), SR}; build_into(Ann, Clauses, Expr, Into, Uniq, S) -> {Fun, SF} = build_var(Ann, S), {Acc, SA} = build_var(Ann, SF),