Skip to content

Commit 953826e

Browse files
committed
remove canonical optimization
1 parent d26031a commit 953826e

File tree

2 files changed

+14
-44
lines changed

2 files changed

+14
-44
lines changed

assets/js/phoenix_live_view/rendered.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -471,27 +471,12 @@ export default class Rendered {
471471
}
472472

473473
comprehensionToBuffer(rendered, templates, output, changeTracking) {
474+
console.log("rendering keyed", structuredClone(rendered));
474475
const keyedTemplates = templates || rendered[TEMPLATES];
475476
const statics = this.templateStatic(rendered[STATIC], templates);
476477
rendered[STATIC] = statics;
477478
delete rendered[TEMPLATES];
478-
let canonicalDiff;
479479
for (let i = 0; i < rendered[KEYED][KEYED_COUNT]; i++) {
480-
// this is another optimization where we assume the first element in
481-
// the comprehension has a "canonical diff" that is shared with all
482-
// following elements (if possible). The diff only contains the
483-
// dynamic parts for the parts that can be shared, therefore we use
484-
// cloneMerge to copy all eligilbe statics from the first diff into
485-
// all subsequent ones.
486-
if (i == 0) {
487-
canonicalDiff = rendered[KEYED][i];
488-
} else {
489-
rendered[KEYED][i] = this.cloneMerge(
490-
canonicalDiff,
491-
rendered[KEYED][i],
492-
true,
493-
);
494-
}
495480
output.buffer += statics[0];
496481
for (let j = 1; j < statics.length; j++) {
497482
this.dynamicToBuffer(

lib/phoenix_live_view/diff.ex

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,11 @@ defmodule Phoenix.LiveView.Diff do
6262
if !keyed or keyed[@keyed_count] == 0 do
6363
{[], components}
6464
else
65-
{diff, {components, _}} =
66-
Enum.map_reduce(0..(keyed[@keyed_count] - 1), {components, nil}, fn
67-
index, {components, canonical_diff} ->
68-
diff = Map.fetch!(keyed, index)
69-
canonical_diff = canonical_diff || diff
70-
# merge nested statics from canonical diff
71-
diff = deep_merge(canonical_diff, diff)
72-
73-
{iodata, components} =
74-
to_iodata(Map.put(diff, @static, static), components, template, mapper)
75-
76-
{iodata, {components, canonical_diff}}
77-
end)
65+
Enum.map_reduce(0..(keyed[@keyed_count] - 1), components, fn index, components ->
66+
diff = Map.fetch!(keyed, index)
7867

79-
{diff, components}
68+
to_iodata(Map.put(diff, @static, static), components, template, mapper)
69+
end)
8070
end
8171
end
8272

@@ -652,13 +642,13 @@ defmodule Phoenix.LiveView.Diff do
652642
diff = %{}
653643
new_prints = %{}
654644

655-
{{diff, count, new_prints, pending, components, template, _canonical_print}, _seen_keys} =
645+
{{diff, count, new_prints, pending, components, template}, _seen_keys} =
656646
Enum.reduce(
657647
entries,
658-
{{diff, 0, new_prints, pending, components, template, nil}, MapSet.new()},
648+
{{diff, 0, new_prints, pending, components, template}, MapSet.new()},
659649
fn
660650
{key, vars, render},
661-
{{_diff, index, _new_prints, _pending, _components, _template, _canonical_print} = acc,
651+
{{_diff, index, _new_prints, _pending, _components, _template} = acc,
662652
seen_keys} ->
663653
{key, seen_keys} =
664654
cond do
@@ -689,7 +679,7 @@ defmodule Phoenix.LiveView.Diff do
689679
# it's an existing entry
690680
defp process_keyed({key, new_vars, render}, previous_prints, changed?, stream?, acc)
691681
when is_map_key(previous_prints, key) and not stream? do
692-
{diff, index, new_prints, pending, components, template, canonical_print} = acc
682+
{diff, index, new_prints, pending, components, template} = acc
693683

694684
%{vars: previous_vars, index: previous_index, child_prints: child_prints} =
695685
Map.fetch!(previous_prints, key)
@@ -711,16 +701,14 @@ defmodule Phoenix.LiveView.Diff do
711701
changed?
712702
)
713703

714-
canonical_print = canonical_print || child_prints
715-
716704
new_prints =
717705
Map.put(new_prints, key, %{index: index, vars: new_vars, child_prints: child_prints})
718706

719707
# if the diff is empty, we need to check if the item moved
720708
if child_diff == %{} or child_diff == nil do
721709
# check if the entry moved, then annotate it with the previous index
722710
diff = if previous_index != index, do: Map.put(diff, index, previous_index), else: diff
723-
{diff, index + 1, new_prints, pending, components, template, canonical_print}
711+
{diff, index + 1, new_prints, pending, components, template}
724712
else
725713
child_diff =
726714
if previous_index != index do
@@ -729,19 +717,18 @@ defmodule Phoenix.LiveView.Diff do
729717
child_diff
730718
end
731719

732-
{Map.put(diff, index, child_diff), index + 1, new_prints, pending, components, template,
733-
canonical_print}
720+
{Map.put(diff, index, child_diff), index + 1, new_prints, pending, components, template}
734721
end
735722
end
736723

737724
# it's a new entry
738725
defp process_keyed({key, vars, render}, _previous_prints, _changed?, stream?, acc) do
739-
{diff, index, new_prints, pending, components, template, canonical_print} = acc
726+
{diff, index, new_prints, pending, components, template} = acc
740727

741728
{_counter, child_diff, child_prints, pending, components, template} =
742729
traverse_dynamic(
743730
render.(%{}, false),
744-
if(canonical_print, do: canonical_print, else: %{}),
731+
%{},
745732
pending,
746733
components,
747734
template,
@@ -750,8 +737,6 @@ defmodule Phoenix.LiveView.Diff do
750737
false
751738
)
752739

753-
canonical_print = canonical_print || child_prints
754-
755740
# if this is a stream, we don't store any fingerprints
756741
new_prints =
757742
if stream? do
@@ -762,7 +747,7 @@ defmodule Phoenix.LiveView.Diff do
762747

763748
diff = Map.put(diff, index, child_diff)
764749

765-
{diff, index + 1, new_prints, pending, components, template, canonical_print}
750+
{diff, index + 1, new_prints, pending, components, template}
766751
end
767752

768753
defp maybe_share_template(map, fingerprint, static, {print_to_pos, pos_to_static}) do

0 commit comments

Comments
 (0)