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
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ Microsoft.Maui.Controls.Label.IsTextSelectable.set -> void
~Microsoft.Maui.Controls.Style.LazyInitialization.set -> void
~Microsoft.Maui.Controls.Internals.TypedBinding<TSource, TProperty>.TypedBinding(System.Func<TSource, (TProperty value, bool success)> getter, System.Action<TSource, TProperty> setter, int listenerCount, Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource> subscriber) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners
Microsoft.Maui.Controls.Internals.TypedBindingListeners.Listen(object? part, string! propertyName) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners.Listen<T>(T part, string! propertyName) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners.TypedBindingListeners() -> void
Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource>
virtual Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource>.Invoke(TSource source, ref Microsoft.Maui.Controls.Internals.TypedBindingListeners listeners) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ Microsoft.Maui.Controls.Label.IsTextSelectable.set -> void
~Microsoft.Maui.Controls.Style.LazyInitialization.set -> void
~Microsoft.Maui.Controls.Internals.TypedBinding<TSource, TProperty>.TypedBinding(System.Func<TSource, (TProperty value, bool success)> getter, System.Action<TSource, TProperty> setter, int listenerCount, Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource> subscriber) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners
Microsoft.Maui.Controls.Internals.TypedBindingListeners.Listen(object? part, string! propertyName) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners.Listen<T>(T part, string! propertyName) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners.TypedBindingListeners() -> void
Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource>
virtual Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource>.Invoke(TSource source, ref Microsoft.Maui.Controls.Internals.TypedBindingListeners listeners) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ Microsoft.Maui.Controls.Label.IsTextSelectable.set -> void
~Microsoft.Maui.Controls.Style.LazyInitialization.set -> void
~Microsoft.Maui.Controls.Internals.TypedBinding<TSource, TProperty>.TypedBinding(System.Func<TSource, (TProperty value, bool success)> getter, System.Action<TSource, TProperty> setter, int listenerCount, Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource> subscriber) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners
Microsoft.Maui.Controls.Internals.TypedBindingListeners.Listen(object? part, string! propertyName) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners.Listen<T>(T part, string! propertyName) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners.TypedBindingListeners() -> void
Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource>
virtual Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource>.Invoke(TSource source, ref Microsoft.Maui.Controls.Internals.TypedBindingListeners listeners) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ Microsoft.Maui.Controls.Label.IsTextSelectable.set -> void
~Microsoft.Maui.Controls.Style.LazyInitialization.set -> void
~Microsoft.Maui.Controls.Internals.TypedBinding<TSource, TProperty>.TypedBinding(System.Func<TSource, (TProperty value, bool success)> getter, System.Action<TSource, TProperty> setter, int listenerCount, Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource> subscriber) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners
Microsoft.Maui.Controls.Internals.TypedBindingListeners.Listen(object? part, string! propertyName) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners.Listen<T>(T part, string! propertyName) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners.TypedBindingListeners() -> void
Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource>
virtual Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource>.Invoke(TSource source, ref Microsoft.Maui.Controls.Internals.TypedBindingListeners listeners) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ Microsoft.Maui.Controls.Label.IsTextSelectable.set -> void
~Microsoft.Maui.Controls.Style.LazyInitialization.set -> void
~Microsoft.Maui.Controls.Internals.TypedBinding<TSource, TProperty>.TypedBinding(System.Func<TSource, (TProperty value, bool success)> getter, System.Action<TSource, TProperty> setter, int listenerCount, Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource> subscriber) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners
Microsoft.Maui.Controls.Internals.TypedBindingListeners.Listen(object? part, string! propertyName) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners.Listen<T>(T part, string! propertyName) -> void
Microsoft.Maui.Controls.Internals.TypedBindingListeners.TypedBindingListeners() -> void
Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource>
virtual Microsoft.Maui.Controls.Internals.TypedBindingSubscriber<TSource>.Invoke(TSource source, ref Microsoft.Maui.Controls.Internals.TypedBindingListeners listeners) -> void
2 changes: 1 addition & 1 deletion src/Controls/src/Core/TypedBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ public TypedBinding(Func<TSource, (TProperty value, bool success)> getter, Actio
/// <param name="setter">Writes the value of the binding path, or <see langword="null"/> when the path is read-only.</param>
/// <param name="listenerCount">
/// The number of objects in the path that <paramref name="subscriber"/> reads, counting the source, which is the most
/// times it calls <see cref="TypedBindingListeners.Listen(object, string)"/>. When it is 1 and the subscriber observes
/// times it calls <see cref="TypedBindingListeners.Listen{T}(T, string)"/>. When it is 1 and the subscriber observes
/// the source itself, notifications from the source don't run <paramref name="subscriber"/> again.
/// </param>
/// <param name="subscriber">Reports the observed objects and properties, or <see langword="null"/> when the binding doesn't observe changes.</param>
Expand Down
38 changes: 33 additions & 5 deletions src/Controls/src/Core/TypedBindingListeners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace Microsoft.Maui.Controls.Internals
{
/// <summary>Reports the objects and properties that a compiled binding observes on <paramref name="source"/>.</summary>
/// <param name="source">The binding source. It is never <see langword="null"/> when called by the binding.</param>
/// <param name="listeners">Receives a <see cref="TypedBindingListeners.Listen(object?, string)"/> call for every observed property, in path order.</param>
/// <param name="listeners">Receives a <see cref="TypedBindingListeners.Listen{T}(T, string)"/> call for every observed property, in path order.</param>
/// <remarks>
/// A subscriber reads the objects of the binding path from the source, calls
/// <see cref="TypedBindingListeners.Listen(object?, string)"/> for each of them before it reads the next one, and returns
/// <see cref="TypedBindingListeners.Listen{T}(T, string)"/> for each of them before it reads the next one, and returns
/// when an object of the path is <see langword="null"/>. The binding runs it on every apply and whenever an observed
/// property changes, so it must report the same calls for the same objects.
/// </remarks>
Expand Down Expand Up @@ -44,15 +44,23 @@ internal int Position
}

/// <summary>Observes <paramref name="propertyName"/> on <paramref name="part"/> when it implements <see cref="INotifyPropertyChanged"/>.</summary>
/// <typeparam name="T">The type of <paramref name="part"/>.</typeparam>
/// <param name="part">The object that owns the observed property.</param>
/// <param name="propertyName">The name of the observed property.</param>
/// <remarks>
/// Each call occupies the next position of the path, even when <paramref name="part"/> can't notify, so that a
/// position keeps its subscription while the same object stays at that position.
/// position keeps its subscription while the same object stays at that position. A value type that doesn't
/// implement <see cref="INotifyPropertyChanged"/> isn't boxed.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Listen(object? part, string propertyName) =>
_subscriptions?.Listen(_walk, _position++, part, propertyName);
public void Listen<T>(T part, string propertyName)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAUI Autopilot · round 1

❌ Not changed. Restoring Listen(object?, string) would reverse the deliberate removal of an unshipped overload in 2481e06. The null-literal source break is real, but generated subscribers do not use that call, and no shipped API signature is being removed. Roslyn also rejects a direct user-defined ref-struct conversion to object (CS0553). Whether to preserve compatibility for prerelease callers is a maintainer decision.

{
// The JIT evaluates the condition for each value type, and removes it for reference types.
if (typeof(T).IsValueType && part is not INotifyPropertyChanged)
_subscriptions?.ListenToNothing(_walk, _position++);
else
_subscriptions?.Listen(_walk, _position++, part, propertyName);
}
}

/// <summary>
Expand Down Expand Up @@ -268,6 +276,24 @@ public void Listen(int walk, int position, object? part, string propertyName)
ListenSlow(walk, position, part, propertyName);
}

// Reports a position whose object can't notify. Generated subscribers report objects of one type at a position, so a
// value type that can't notify never finds a handler at its position. Any other subscriber may have reported an
// object that notifies there in an earlier walk: its handler is removed.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ListenToNothing(int walk, int position)
{
var listeners = _listeners;
if ((uint)position < (uint)listeners.Length && listeners[position] is { IsDetached: false } listener)
DetachSlow(walk, position, listener);
}

[MethodImpl(MethodImplOptions.NoInlining)]
void DetachSlow(int walk, int position, Listener listener)
{
if (walk == _walk && TryDetach(listener, position) is { } failure)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[moderate] Regression Prevention and Test Coverage - The new walk-ownership guard has no discriminating superseded-walk test through ListenToNothing. A handwritten subscriber can read a Frame through a getter that raises a root notification: the nested walk observes a notifying child at that position, then the outer walk resumes with Listen<Frame>. That stale call must leave the newer child's handler intact. Without this guard it detaches the handler, so subsequent child changes stop updating the target. The new theory exercises only current walks; WalkSupersededByNestedWalkKeepsItsSubscriptions uses object-returning PathPart delegates and never reaches DetachSlow. Add a typed-Frame reentrancy case asserting that the child remains subscribed and still updates the target.

_failure ??= failure;
}

[MethodImpl(MethodImplOptions.NoInlining)]
void ListenSlow(int walk, int position, object? part, string propertyName)
{
Expand Down Expand Up @@ -441,6 +467,8 @@ public Listener(WeakReference<TypedBindingSubscriptions> owner)

public string? PropertyName { get; set; }

public bool IsDetached => _state == Detached;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsListening(object? part, string propertyName) =>
ReferenceEquals(PropertyName, propertyName) && IsListeningTo(part);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,52 @@ public void EqualPropertyNamesDoNotResubscribe()
Assert.Equal(1, vm.ListenerCount);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void ValueThatCannotNotifyRemovesHandlerAtItsPosition(bool failFirstRemoval)
{
var child = new Node { Name = "child" };
var vm = new Node { Child = child };
var observeChild = true;
var binding = new TypedBinding<Node, string?>(
static source => (source.Child?.Name, true),
null,
listenerCount: 2,
(Node source, ref TypedBindingListeners listeners) =>
{
listeners.Listen(source, nameof(Node.Child));
if (observeChild)
listeners.Listen(source.Child, nameof(Node.Name));
else
listeners.Listen(new Frame { Child = source.Child }, nameof(Frame.Child));
});
var bindable = Bind(binding, vm);
Assert.Equal(1, child.ListenerCount);

if (failFirstRemoval)
child.BeforeUnsubscribe = () =>
{
child.BeforeUnsubscribe = null;
throw new InvalidOperationException("Event removal failed");
};

observeChild = false;
vm.Raise(nameof(Node.Child));
Comment thread
github-actions[bot] marked this conversation as resolved.
Resolved
Comment thread
github-actions[bot] marked this conversation as resolved.
Resolved

if (failFirstRemoval)
{
Assert.Equal(1, child.Unsubscribes);
Assert.Equal(1, child.ListenerCount);
vm.Raise(nameof(Node.Child));
Assert.Equal(2, child.Unsubscribes);
}

Assert.Equal(0, child.ListenerCount);
Assert.Equal(1, vm.ListenerCount);
GC.KeepAlive(bindable);
}

[Fact]
public void LegacyNullHandlerKeepsTheFollowingPositions()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#nullable enable
using System.ComponentModel;
using BenchmarkDotNet.Attributes;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui.Benchmarks
{
/// <summary>
/// Measures source-generated bindings whose path goes through a struct, as <c>Bounds.Width</c> goes through
/// <see cref="Rect"/>. The generators observe a struct that another assembly or the app defines, because a version of
/// it could implement <see cref="INotifyPropertyChanged"/>.
/// </summary>
[MemoryDiagnoser]
public class TypedBindingStructPathBenchmarker
{
const int Iterations = 100;

static readonly Rect Small = new(0, 0, 10, 10);
static readonly Rect Large = new(0, 0, 20, 20);

readonly Item _first = new() { Bounds = Small };
readonly Item _second = new() { Bounds = Large };
readonly Target _target = new();
readonly Target _unboundTarget = new();

[GlobalSetup]
public void Setup()
{
SynchronousDispatcherProvider.Register();
_target.BindingContext = _first;
_target.SetBinding(Target.WidthProperty, CreateBinding());
_unboundTarget.BindingContext = _first;
}

[GlobalCleanup]
public void Cleanup() => SynchronousDispatcherProvider.Unregister();

static BindingBase CreateBinding() => BindingBase.Create(static (Item item) => item.Bounds.Width);

// The struct is replaced, so the binding reads the path again.
[Benchmark]
public void StructUpdate()
{
for (int i = 0; i < Iterations; i++)
_first.Bounds = (i & 1) == 0 ? Large : Small;
}

// The whole path changes, as when a recycled cell gets a new item.
[Benchmark]
public void ContextSwap()
{
for (int i = 0; i < Iterations; i++)
_target.BindingContext = (i & 1) == 0 ? _second : _first;
}

// A binding is created, applied to a target with a binding context, and removed.
[Benchmark]
public void CreateApplyAndRemove()
{
for (int i = 0; i < Iterations; i++)
{
_unboundTarget.SetBinding(Target.WidthProperty, CreateBinding());
_unboundTarget.RemoveBinding(Target.WidthProperty);
}
}

public sealed class Item : INotifyPropertyChanged
{
static readonly PropertyChangedEventArgs BoundsChanged = new(nameof(Bounds));
Rect _bounds;

public event PropertyChangedEventHandler? PropertyChanged;

public Rect Bounds
{
get => _bounds;
set
{
_bounds = value;
PropertyChanged?.Invoke(this, BoundsChanged);
}
}
}

sealed class Target : BindableObject
{
public static readonly BindableProperty WidthProperty = BindableProperty.Create("Width", typeof(double), typeof(Target), 0d);
}
}
}
Loading