Skip to content

Commit 6421959

Browse files
precizjosevalim
authored andcommitted
Optimize traverse_dynamic for nil and binary entries
Short-circuit the traversal of nil (unchanged) and binary entries to avoid function call overhead and map lookups.
1 parent 4e9d49c commit 6421959

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

lib/phoenix_live_view/diff.ex

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,29 @@ defmodule Phoenix.LiveView.Diff do
615615
traverse_dynamic(dynamic, 0, %{}, children, pending, components, template, changed?)
616616
end
617617

618+
defp traverse_dynamic(
619+
[entry | entries],
620+
counter,
621+
diff,
622+
children,
623+
pending,
624+
components,
625+
template,
626+
changed?
627+
)
628+
when is_nil(entry) or (is_binary(entry) and not is_map_key(children, counter)) do
629+
traverse_dynamic(
630+
entries,
631+
counter + 1,
632+
if(is_binary(entry), do: Map.put(diff, counter, entry), else: diff),
633+
children,
634+
pending,
635+
components,
636+
template,
637+
changed?
638+
)
639+
end
640+
618641
defp traverse_dynamic(
619642
[entry | entries],
620643
counter,

0 commit comments

Comments
 (0)