Skip to content

Conversation

sabiwara
Copy link
Contributor

Follow-up for #14774.

Fixes the discrepancy found in the previous benchmark: sabiwara/elixir_benches@cd1c9a3.

Before
Screenshot 2025-09-29 at 15 38 13

After
Screenshot 2025-09-29 at 15 41 53

Note: this is basically the same as building a list, but we don't need the lists:reverse step and replace it with MapSet.new/1.

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!

@sabiwara sabiwara merged commit 16fb815 into elixir-lang:main Sep 29, 2025
13 checks passed
@sabiwara sabiwara deleted the opti-for-into-mapset branch September 29, 2025 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants