Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/Reactor/Core/Reconciler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,78 @@ public static void SetElementTag(FrameworkElement control, Element? element)
control.SetValue(ReactorAttached.StateProperty, state);
}

/// <summary>
/// Spec 047 §4.4 follow-up — allocation-gated element tagging for the V1
/// dispatch adapter. Refreshes an existing <see cref="ReactorState"/>'s
/// element pointer cheaply, but only <em>allocates</em> a new
/// <see cref="ReactorState"/> (+ attached-DP write) for elements whose
/// downstream code actually reads the element back via
/// <see cref="GetElementTag(FrameworkElement)"/>.
///
/// <para>Three things require a live element pointer on the control:</para>
/// <list type="bullet">
/// <item>
/// <b>Event trampolines</b> — <see cref="Element.HasCallbacks"/>.
/// Routed-input modifiers (<c>.OnPointerPressed</c> etc.) dispatch
/// through <see cref="ModifierEventHandlerState"/>'s <c>Current*</c>
/// fields refreshed each render by <c>ApplyEventHandlers</c> and do
/// <em>not</em> resolve via the tag, so they are intentionally not
/// part of the gate.
/// </item>
/// <item>
/// <b>Keyed child reconciliation</b> — <see cref="Element.Key"/>.
/// <see cref="ChildReconciler"/> reads the child's tag to extract the
/// stable key during keyed reorders (see <c>ChildReconciler.cs:303</c>
/// and <c>:392</c>). A keyed callback-free element (e.g.
/// <c>Border(...).WithKey("a")</c>) loses its identity across reorders
/// without the tag.
/// </item>
/// <item>
/// <b>Element-extras readback</b> — <see cref="Element.Extensions"/>.
/// Several bucketed extras have engine paths that read the live
/// element via the tag — exit transitions
/// (<c>ElementTransition</c>), connected-animation snapshots
/// (<c>ConnectedAnimationKey</c>), <c>InteractionStates</c>,
/// <c>KeyframeAnimations</c>, and friends. The bucket being null is
/// spec 047 §4.4's "truly inert leaf" signal, so we tag whenever any
/// extra is present rather than enumerating each feature.
/// </item>
/// </list>
///
/// <para>Callback-free display leaves with no key and no extras
/// (TextBlock / Border / StackPanel / Image — the bulk of a real tree)
/// never dispatch into Reactor code and have nothing reading the tag, so
/// they stay untagged. This mirrors the legacy reconciler discipline. The
/// pre-§4.5 V1 adapter tagged <em>every</em> control unconditionally,
/// allocating a ReactorState per leaf mount — the per-render byte
/// regression this restores the savings for.</para>
/// </summary>
internal static void SetElementTagIfNeeded(FrameworkElement control, Element element)
{
if (control.GetValue(ReactorAttached.StateProperty) is ReactorState state)
{
// State already allocated (callback-bearing control, pooled reuse,
// or echo/setter scope) — refresh the live element with no alloc.
state.Element = element;
return;
}
if (!NeedsTag(element)) return;
control.SetValue(
ReactorAttached.StateProperty,
new ReactorState { Element = element });
}
Comment on lines +757 to +770

/// <summary>
/// Predicate companion to <see cref="SetElementTagIfNeeded"/>. Returns
/// true when downstream code will read the element back through
/// <see cref="GetElementTag(FrameworkElement)"/> — see the helper's
/// summary for the three categories.
/// </summary>
private static bool NeedsTag(Element element) =>
element.HasCallbacks
|| element.Key is not null
|| element.Extensions is not null;

/// <summary>
/// Spec 047 §14 Phase 1 (1.3) — promoted from internal. Retrieves the
/// element associated with a control, or null.
Expand Down
6 changes: 4 additions & 2 deletions src/Reactor/Core/V1Protocol/V1HandlerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public UIElement Mount(Element element, Action requestRerender, Reconciler recon

// Anchor element identity on the control via the attached state DP so
// event trampolines can re-fetch the live element on each fire.
// Gated: callback-free leaves never dispatch into Reactor code, so we
// skip the ReactorState allocation for them (§4.4 follow-up).
if (control is FrameworkElement fe)
Reconciler.SetElementTag(fe, typedEl);
Reconciler.SetElementTagIfNeeded(fe, typedEl);

// Strategy dispatch — only when the handler declares a non-None Children strategy.
var strategy = _handler.Children;
Expand All @@ -62,7 +64,7 @@ public UIElement Update(Element oldEl, Element newEl, UIElement control, Action
_handler.Update(ctx, typedOld, typedNew, typedControl);

if (control is FrameworkElement fe)
Reconciler.SetElementTag(fe, typedNew);
Reconciler.SetElementTagIfNeeded(fe, typedNew);

var strategy = _handler.Children;
if (strategy is not null)
Expand Down
Loading