Repository navigation
Don't box structs in compiled binding subscriptions #40849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c043c68
b242d5d
4de66a2
2481e06
0104c7d
9d1e187
c8395b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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> | ||
|
|
@@ -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) | ||
| { | ||
| // 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> | ||
|
|
@@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| _failure ??= failure; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| void ListenSlow(int walk, int position, object? part, string propertyName) | ||
| { | ||
|
|
@@ -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); | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
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 in2481e06. 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 toobject(CS0553). Whether to preserve compatibility for prerelease callers is a maintainer decision.