Skip to content

Commit 34c0f64

Browse files
authored
Optimize keyed_to_iodata by iterating backward (#4197)
Iterating backward avoids Enum.reverse/1, simplifies recursion, and speeds up rendering of large comprehensions.
1 parent c433ea0 commit 34c0f64

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

lib/phoenix_live_view/diff.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ defmodule Phoenix.LiveView.Diff do
6262
if !keyed or keyed[@keyed_count] == 0 do
6363
{[], components}
6464
else
65-
keyed_to_iodata(0, keyed[@keyed_count], keyed, static, components, template, mapper, [])
65+
keyed_to_iodata(keyed[@keyed_count] - 1, keyed, static, components, template, mapper, [])
6666
end
6767
end
6868

@@ -82,15 +82,15 @@ defmodule Phoenix.LiveView.Diff do
8282
{binary, components}
8383
end
8484

85-
defp keyed_to_iodata(index, limit, keyed, static, components, template, mapper, acc)
86-
when index < limit do
85+
defp keyed_to_iodata(index, keyed, static, components, template, mapper, acc)
86+
when index >= 0 do
8787
diff = Map.fetch!(keyed, index)
8888
{iodata, components} = to_iodata(Map.put(diff, @static, static), components, template, mapper)
89-
keyed_to_iodata(index + 1, limit, keyed, static, components, template, mapper, [iodata | acc])
89+
keyed_to_iodata(index - 1, keyed, static, components, template, mapper, [iodata | acc])
9090
end
9191

92-
defp keyed_to_iodata(_index, _limit, _keyed, _static, components, _template, _mapper, acc) do
93-
{Enum.reverse(acc), components}
92+
defp keyed_to_iodata(_index, _keyed, _static, components, _template, _mapper, acc) do
93+
{acc, components}
9494
end
9595

9696
defp one_to_iodata([last], _parts, _counter, acc, components, _template, _mapper) do

0 commit comments

Comments
 (0)