Skip to content

Commit 3c6d699

Browse files
authored
Optimize keyed_to_iodata by extracting to_iodata_parts to avoid Map.put (#4198)
Extracts `to_iodata_parts/5` from `to_iodata/4` to bypass an unnecessary `Map.put(diff, @static, static)` allocation on every iteration of a comprehension. Benchmarks show a ~1.7x speedup for converting comprehensions into iodata (1833ms -> 1074ms for 100k iterations of a 100-item comprehension).
1 parent 3179652 commit 3c6d699

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lib/phoenix_live_view/diff.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ defmodule Phoenix.LiveView.Diff do
6767
end
6868

6969
defp to_iodata(%{@static => static} = parts, components, template, mapper) do
70-
static = template_static(static, template)
71-
one_to_iodata(static, parts, 0, [], components, template, mapper)
70+
to_iodata_parts(parts, static, components, template, mapper)
7271
end
7372

7473
defp to_iodata(cid, components, _template, mapper) when is_integer(cid) do
@@ -82,10 +81,15 @@ defmodule Phoenix.LiveView.Diff do
8281
{binary, components}
8382
end
8483

84+
defp to_iodata_parts(parts, static, components, template, mapper) do
85+
static = template_static(static, template)
86+
one_to_iodata(static, parts, 0, [], components, template, mapper)
87+
end
88+
8589
defp keyed_to_iodata(index, keyed, static, components, template, mapper, acc)
8690
when index >= 0 do
8791
diff = Map.fetch!(keyed, index)
88-
{iodata, components} = to_iodata(Map.put(diff, @static, static), components, template, mapper)
92+
{iodata, components} = to_iodata_parts(diff, static, components, template, mapper)
8993
keyed_to_iodata(index - 1, keyed, static, components, template, mapper, [iodata | acc])
9094
end
9195

0 commit comments

Comments
 (0)