Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/elixir/src/elixir_erl_for.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down Expand Up @@ -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};
Copy link
Member

Choose a reason for hiding this comment

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

I think we could even optimize this further by changing the comprehension to return {Item, true} and then building the structure directly.

Copy link
Contributor Author

@sabiwara sabiwara Sep 29, 2025

Choose a reason for hiding this comment

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

Oh, do you mean this:

%MapSet{map: :maps.from_list(Enum.reduce([1, 2, 3], [], fn x, var3 -> [{x, []} | var3] end))}

Makes sense!

({Item, []}, not {Item, true}, right?)

Copy link
Contributor Author

@sabiwara sabiwara Sep 29, 2025

Choose a reason for hiding this comment

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

OK, interesting.

Done in c713cde, but while it speeds things up, it also seems to increase memory significantly. Maybe not worth it? sabiwara/elixir_benches@14c0876

(maybe because you implemented :sets.from_list(version: 2) efficiently as a BIF, and it doesn't need all these tuples?)

Screenshot 2025-09-29 at 17 07 17

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will revert to the previous version and merge, WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

Keep the new version but use from_keys without the tuple. Although I guess that’s the same as MapSet.new anyway? So yeah, please revert!

build_into(Ann, Clauses, Expr, Into, Uniq, S) ->
{Fun, SF} = build_var(Ann, S),
{Acc, SA} = build_var(Ann, SF),
Expand Down