Skip to content

Commit 447d163

Browse files
authored
Optimize MapSet to Map in traverse_keyed (#4193)
Using a simple map avoids the overhead of the %MapSet{} struct and its function calls, providing a ~38% speedup in checking duplicate keys during comprehension rendering.
1 parent 52c9d3b commit 447d163

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

lib/phoenix_live_view/diff.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ defmodule Phoenix.LiveView.Diff do
652652
{{diff, count, new_prints, pending, components, template}, _seen_keys} =
653653
Enum.reduce(
654654
entries,
655-
{{diff, 0, new_prints, pending, components, template}, MapSet.new()},
655+
{{diff, 0, new_prints, pending, components, template}, %{}},
656656
fn
657657
{key, vars, render},
658658
{{_diff, index, _new_prints, _pending, _components, _template} = acc, seen_keys} ->
@@ -662,11 +662,11 @@ defmodule Phoenix.LiveView.Diff do
662662
# no need to check for duplicates if we use the index
663663
{index, seen_keys}
664664

665-
MapSet.member?(seen_keys, key) ->
665+
Map.has_key?(seen_keys, key) ->
666666
raise "found duplicate key #{inspect(key)} in comprehension"
667667

668668
true ->
669-
{key, MapSet.put(seen_keys, key)}
669+
{key, Map.put(seen_keys, key, true)}
670670
end
671671

672672
{process_keyed({key, vars, render}, previous_prints, changed?, stream?, acc),

0 commit comments

Comments
 (0)