Repository navigation
[Perf] Share PropertyChanged handlers between compiled bindings of an object - #40903
simonrozsival wants to merge 8 commits into
Conversation
Every compiled binding (TypedBinding) added its own PropertyChanged handler to each object on its path. The bindings of an item template all observe the item, so a recycled row removed and added one handler per binding on every rebind. Each add or remove copies the invocation list of the event, so the cost grows with the square of the number of bindings per object. In the CollectionView scroll benchmark (13 compiled bindings per row) this churn was 13% of the bytes that the UI thread allocates on Android. Listeners of the same object now share one handler that fans out to them: - The first listener of an object adds its own handler, as before, and the thread remembers it in a short list of the objects it observed last (16 entries). - The next listener of that object registers a shared subscription, which the later ones join. The first listener keeps its own handler on that object, and shares from the next object it observes on. - A listener whose object no other listener observed keeps adding its own handler without looking, so an object with one binding costs what it did. Value types, which every walk boxes again, always use their own handler. - The last listener to leave removes the shared handler. A freed subscription is pooled by its thread and reused, with its weak reference. A recycled row now moves one handler per object instead of one per binding. Preserved behavior: - Property names are filtered per listener: null or empty means all properties, and indexer names match as before. - A binding removed during a notification isn't notified by it. A binding added during a notification is notified by the next one. Bindings moved to another object during a notification aren't notified by it. - Memory: the object references the shared handler, the handler its listeners, and each listener its binding only weakly, as before. The listeners of a collected binding leave through its finalizer or on the next notification, and the last one removes the handler. - Dispatching is unchanged. Only the thread that registered a shared subscription adds listeners to it; any thread, including the finalizer thread, removes them, under a lock. - When the event accessor throws, the next walk retries, as before. Ordering: the bindings that share a handler are notified in the order in which they joined, at the position of the shared handler. So the third and later bindings of an object now run before a non-binding handler that was added between the second and the third binding. The first binding keeps its own position. When a thread observes more objects between two bindings of an object than it remembers, the bindings don't find each other, and each adds its own handler, as before. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Expert Review — 3 findings · ⚠️ 1 breaking change
Reviewed commit da8e793.
Warning
Existing apps would look or behave differently without opting in. See the Behavioral Change Disclosure findings at:
See inline comments for details.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Expert Review — No more findings
Reviewed commit 147e7f8.
Selected Tests🧪 Test selection — click to expand
🧪 UI tests — the full suite on every platform❌ Could not queue GPT-6.2 Sol: Compiled-binding listeners now share PropertyChanged handlers, so run CollectionView and CarouselView UI tests on all four platforms. 🧪 Running — 77 categories on Android, iOS, MacCatalyst, WindowsThe complete 🧪 Device tests —
|
| Platform | Categories |
|---|---|
| Android | Controls.Accessibility, Controls.Animations, Controls.Application, Controls.Behavior, Controls.Border, Controls.BoxView, Controls.Button, Controls.CarouselView, Controls.CheckBox, Controls.CollectionView, Controls.Compatibility, Controls.ContentView, Controls.DatePicker, Controls.Dispatcher, Controls.Editor, Controls.Element, Controls.Entry, Controls.FlexLayout, Controls.FlyoutPage, Controls.Frame, Controls.Gesture, Controls.HybridWebView, Controls.Image, Controls.Label, Controls.Layout, Controls.Lifecycle, Controls.Map, Controls.Mapper, Controls.Memory, Controls.MenuFlyout, Controls.Modal, Controls.NavigationPage, Controls.Page, Controls.PageTransitions, Controls.Path, Controls.Picker, Controls.RadioButton, Controls.RefreshView, Controls.ScrollView, Controls.SearchBar, Controls.Shape, Controls.Shell, Controls.Slider, Controls.SwipeView, Controls.Switch, Controls.TabbedPage, Controls.TemplatedView, Controls.TextInput, Controls.Toolbar, Controls.View, Controls.VisualElement, Controls.VisualElementTree, Controls.WebView, Controls.Window, Controls.WindowOverlay, Controls.Xaml |
| iOS | Controls.Accessibility, Controls.Animations, Controls.Application, Controls.Behavior, Controls.Border, Controls.BoxView, Controls.Button, Controls.CarouselView, Controls.CheckBox, Controls.CollectionView, Controls.Compatibility, Controls.ContentView, Controls.DatePicker, Controls.Dispatcher, Controls.Editor, Controls.Element, Controls.Entry, Controls.FlexLayout, Controls.FlyoutPage, Controls.Frame, Controls.Gesture, Controls.HybridWebView, Controls.Image, Controls.Label, Controls.Layout, Controls.Lifecycle, Controls.Map, Controls.Mapper, Controls.Memory, Controls.MenuFlyout, Controls.Modal, Controls.NavigationPage, Controls.Page, Controls.PageTransitions, Controls.Path, Controls.Picker, Controls.RadioButton, Controls.RefreshView, Controls.ScrollView, Controls.SearchBar, Controls.Shape, Controls.Shell, Controls.Slider, Controls.SwipeView, Controls.Switch, Controls.TabbedPage, Controls.TemplatedView, Controls.TextInput, Controls.Toolbar, Controls.View, Controls.VisualElement, Controls.VisualElementTree, Controls.WebView, Controls.Window, Controls.WindowOverlay, Controls.Xaml |
| MacCatalyst | Controls.Accessibility, Controls.Animations, Controls.Application, Controls.Behavior, Controls.Border, Controls.BoxView, Controls.Button, Controls.CarouselView, Controls.CheckBox, Controls.CollectionView, Controls.Compatibility, Controls.ContentView, Controls.DatePicker, Controls.Dispatcher, Controls.Editor, Controls.Element, Controls.Entry, Controls.FlexLayout, Controls.FlyoutPage, Controls.Frame, Controls.Gesture, Controls.HybridWebView, Controls.Image, Controls.Label, Controls.Layout, Controls.Lifecycle, Controls.Map, Controls.Mapper, Controls.Memory, Controls.MenuFlyout, Controls.Modal, Controls.NavigationPage, Controls.Page, Controls.PageTransitions, Controls.Path, Controls.Picker, Controls.RadioButton, Controls.RefreshView, Controls.ScrollView, Controls.SearchBar, Controls.Shape, Controls.Shell, Controls.Slider, Controls.SwipeView, Controls.Switch, Controls.TabbedPage, Controls.TemplatedView, Controls.TextInput, Controls.Toolbar, Controls.View, Controls.VisualElement, Controls.VisualElementTree, Controls.WebView, Controls.Window, Controls.WindowOverlay, Controls.Xaml |
| Windows | Controls.Accessibility, Controls.Animations, Controls.Application, Controls.Behavior, Controls.Border, Controls.BoxView, Controls.Button, Controls.CarouselView, Controls.CheckBox, Controls.CollectionView, Controls.Compatibility, Controls.ContentView, Controls.DatePicker, Controls.Dispatcher, Controls.Editor, Controls.Element, Controls.Entry, Controls.FlexLayout, Controls.FlyoutPage, Controls.Frame, Controls.Gesture, Controls.HybridWebView, Controls.Image, Controls.Label, Controls.Layout, Controls.Lifecycle, Controls.Map, Controls.Mapper, Controls.Memory, Controls.MenuFlyout, Controls.Modal, Controls.NavigationPage, Controls.Page, Controls.PageTransitions, Controls.Path, Controls.Picker, Controls.RadioButton, Controls.RefreshView, Controls.ScrollView, Controls.SearchBar, Controls.Shape, Controls.Shell, Controls.Slider, Controls.SwipeView, Controls.Switch, Controls.TabbedPage, Controls.TemplatedView, Controls.TextInput, Controls.Toolbar, Controls.View, Controls.VisualElement, Controls.VisualElementTree, Controls.WebView, Controls.Window, Controls.WindowOverlay, Controls.Xaml |
⏭️ Skipped — what neither suite runs
Device tests — 49 categories, 0 platforms
Categories not run: Core.ActivityIndicator, Core.Animation, Core.Application, Core.Border, Core.BoxView, Core.Button, Core.CheckBox, Core.ContentView, Core.DatePicker, Core.Dispatcher, Core.Editor, Core.Element, Core.Entry, Core.FlowDirection, Core.FlyoutView, Core.Fonts, Core.Formatting, Core.Graphics, Core.GraphicsView, Core.Image, Core.ImageButton, Core.ImageSource, Core.IndicatorView, Core.Label, Core.Layout, Core.Lifecycle, Core.MauiContext, Core.Memory, Core.NavigationPage, Core.Page, Core.Picker, Core.ProgressBar, Core.RadioButton, Core.RefreshView, Core.ScrollView, Core.SearchBar, Core.ShapeView, Core.Slider, Core.Stepper, Core.SwipeView, Core.Switch, Core.TimePicker, Core.View, Core.WebView, Core.Window, Core.WindowOverlay, Graphics, Essentials, BlazorWebView
Each skipped platform and project is listed as a maui-pr-devicetests (skipped) … status on this commit. Use /azp run maui-pr-devicetests for the full suite.
🤖 Why these tests — AI rationale (GPT-6.2 Sol + MAI Code 2)
UI tests
Each model chose independently; every test that runs was selected by at least one of them.
| Platform | GPT-6.2 Sol | MAI Code 2 |
|---|---|---|
| Android | CollectionView1, CollectionView2, CollectionView3, CollectionView4, CollectionView5, CollectionView6, CollectionView7, CarouselView |
all categories |
| iOS | CollectionView1, CollectionView2, CollectionView3, CollectionView4, CollectionView5, CollectionView6, CollectionView7, CarouselView |
all categories |
| MacCatalyst | CollectionView1, CollectionView2, CollectionView3, CollectionView4, CollectionView5, CollectionView6, CollectionView7, CarouselView |
all categories |
| Windows | CollectionView1, CollectionView2, CollectionView3, CollectionView4, CollectionView5, CollectionView6, CollectionView7, CarouselView |
all categories |
GPT-6.2 Sol — Compiled-binding listeners now share PropertyChanged handlers, so run CollectionView and CarouselView UI tests on all four platforms.
- TypedBindingListeners.cs changes notification and detach behavior when CollectionView rows rebind.
- CarouselView item templates also change binding contexts as items are reused.
- No base handler, layout, or build changes warrant a full run.
MAI Code 2 — Cross-platform compiled-binding listener sharing changes behavior for bindings across the Controls UI surface; run the complete UI suite on all platforms and all Controls device tests.
TypedBindingListeners.csis shared Controls binding infrastructure used by compiled bindings across controls, templates, and platforms, so platform-specific narrowing is not safe.- Handler sharing changes notification ordering, attachment/detachment, re-entrancy, and lifetime behavior; that is a broad UI behavior change warranting a complete UI run.
- The compatibility document changes only the switch contract and does not widen the implementation scope.
- The changed tests are unit tests and have no UI test category hints; CollectionView is the motivating performance case, not the only affected consumer.
Device tests
Each model chose independently; every test that runs was selected by at least one of them.
| Platform | GPT-6.2 Sol | MAI Code 2 |
|---|---|---|
| Android | Controls.CollectionView, Controls.CarouselView, Controls.Memory |
Controls.Accessibility, Controls.Animations, Controls.Application, Controls.Behavior, Controls.Border, Controls.BoxView, Controls.Button, Controls.CarouselView, Controls.CheckBox, Controls.CollectionView, Controls.Compatibility, Controls.ContentView, Controls.DatePicker, Controls.Dispatcher, Controls.Editor, Controls.Element, Controls.Entry, Controls.FlexLayout, Controls.FlyoutPage, Controls.Frame, Controls.Gesture, Controls.HybridWebView, Controls.Image, Controls.Label, Controls.Layout, Controls.Lifecycle, Controls.Map, Controls.Mapper, Controls.Memory, Controls.MenuFlyout, Controls.Modal, Controls.NavigationPage, Controls.Page, Controls.PageTransitions, Controls.Path, Controls.Picker, Controls.RadioButton, Controls.RefreshView, Controls.ScrollView, Controls.SearchBar, Controls.Shape, Controls.Shell, Controls.Slider, Controls.SwipeView, Controls.Switch, Controls.TabbedPage, Controls.TemplatedView, Controls.TextInput, Controls.Toolbar, Controls.View, Controls.VisualElement, Controls.VisualElementTree, Controls.WebView, Controls.Window, Controls.WindowOverlay, Controls.Xaml |
| iOS | Controls.CollectionView, Controls.CarouselView, Controls.Memory |
Controls.Accessibility, Controls.Animations, Controls.Application, Controls.Behavior, Controls.Border, Controls.BoxView, Controls.Button, Controls.CarouselView, Controls.CheckBox, Controls.CollectionView, Controls.Compatibility, Controls.ContentView, Controls.DatePicker, Controls.Dispatcher, Controls.Editor, Controls.Element, Controls.Entry, Controls.FlexLayout, Controls.FlyoutPage, Controls.Frame, Controls.Gesture, Controls.HybridWebView, Controls.Image, Controls.Label, Controls.Layout, Controls.Lifecycle, Controls.Map, Controls.Mapper, Controls.Memory, Controls.MenuFlyout, Controls.Modal, Controls.NavigationPage, Controls.Page, Controls.PageTransitions, Controls.Path, Controls.Picker, Controls.RadioButton, Controls.RefreshView, Controls.ScrollView, Controls.SearchBar, Controls.Shape, Controls.Shell, Controls.Slider, Controls.SwipeView, Controls.Switch, Controls.TabbedPage, Controls.TemplatedView, Controls.TextInput, Controls.Toolbar, Controls.View, Controls.VisualElement, Controls.VisualElementTree, Controls.WebView, Controls.Window, Controls.WindowOverlay, Controls.Xaml |
| MacCatalyst | Controls.CollectionView, Controls.CarouselView, Controls.Memory |
Controls.Accessibility, Controls.Animations, Controls.Application, Controls.Behavior, Controls.Border, Controls.BoxView, Controls.Button, Controls.CarouselView, Controls.CheckBox, Controls.CollectionView, Controls.Compatibility, Controls.ContentView, Controls.DatePicker, Controls.Dispatcher, Controls.Editor, Controls.Element, Controls.Entry, Controls.FlexLayout, Controls.FlyoutPage, Controls.Frame, Controls.Gesture, Controls.HybridWebView, Controls.Image, Controls.Label, Controls.Layout, Controls.Lifecycle, Controls.Map, Controls.Mapper, Controls.Memory, Controls.MenuFlyout, Controls.Modal, Controls.NavigationPage, Controls.Page, Controls.PageTransitions, Controls.Path, Controls.Picker, Controls.RadioButton, Controls.RefreshView, Controls.ScrollView, Controls.SearchBar, Controls.Shape, Controls.Shell, Controls.Slider, Controls.SwipeView, Controls.Switch, Controls.TabbedPage, Controls.TemplatedView, Controls.TextInput, Controls.Toolbar, Controls.View, Controls.VisualElement, Controls.VisualElementTree, Controls.WebView, Controls.Window, Controls.WindowOverlay, Controls.Xaml |
| Windows | Controls.CollectionView, Controls.CarouselView, Controls.Memory |
Controls.Accessibility, Controls.Animations, Controls.Application, Controls.Behavior, Controls.Border, Controls.BoxView, Controls.Button, Controls.CarouselView, Controls.CheckBox, Controls.CollectionView, Controls.Compatibility, Controls.ContentView, Controls.DatePicker, Controls.Dispatcher, Controls.Editor, Controls.Element, Controls.Entry, Controls.FlexLayout, Controls.FlyoutPage, Controls.Frame, Controls.Gesture, Controls.HybridWebView, Controls.Image, Controls.Label, Controls.Layout, Controls.Lifecycle, Controls.Map, Controls.Mapper, Controls.Memory, Controls.MenuFlyout, Controls.Modal, Controls.NavigationPage, Controls.Page, Controls.PageTransitions, Controls.Path, Controls.Picker, Controls.RadioButton, Controls.RefreshView, Controls.ScrollView, Controls.SearchBar, Controls.Shape, Controls.Shell, Controls.Slider, Controls.SwipeView, Controls.Switch, Controls.TabbedPage, Controls.TemplatedView, Controls.TextInput, Controls.Toolbar, Controls.View, Controls.VisualElement, Controls.VisualElementTree, Controls.WebView, Controls.Window, Controls.WindowOverlay, Controls.Xaml |
GPT-6.2 Sol — Run targeted Controls device tests for item-template rebinding and listener lifetime on all four platforms.
- CollectionView and CarouselView exercise binding-context changes in item templates.
- Controls.Memory covers retention risks from the new shared weak subscriptions.
- No Core handler or XAML compiler changes; omit their suites.
MAI Code 2 — Run all Controls device-test categories on every platform because shared compiled-binding subscriptions affect controls broadly, without expanding to unrelated Core, Graphics, or Essentials projects.
TypedBindingListeners.csis cross-platform Controls infrastructure, so binding and lifecycle behavior can affect every Controls category on all four platforms.- The Controls umbrella covers binding consumers plus relevant CollectionView and memory/lifecycle scenarios.
Corehandlers, Graphics, Essentials, and BlazorWebView are unchanged; a device full run would add unrelated projects.
Generated by /review run-tests. A targeted run is a fast signal, not full-suite certification.
MAUI Autopilot — Ready for maintainer reviewResults
|
Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
da8e79326c24a5005427517a4fb698867980c4a7↑ 22 new test cases cover sharing, order, re-entrancy, a failed registration, collected bindings and objects, and other threads; 17 of them fail on main's engine.
↑ The scroll benchmark allocates exactly 2,736 B less per row on Android and 3,480 B less on Mac Catalyst.
↓ Handlers run in a different order when bindings share one (documented and tested).
↓ Moving a single binding between objects measured about 2–3% slower on the host, up to +7.5%, near the harness's resolution.
↓ Concurrency is covered by one stress test; no device tests, UI tests or CI results yet.
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Target Branch
mainnet11, for reviewFor .NET 11 fixes, only a maintainer retargets the approved PR to the active
inflight lane and merges it after CI validates that final base. Do not merge
individual fixes directly into
net11; agents must not retarget or merge.Description of Change
This is a draft because the design needs the binding owners' agreement before review. It changes the order in which
PropertyChangedhandlers run when bindings share one (documented and tested below). Also, moving a single binding between objects measured about 2–3% slower on the host (up to +7.5% in one case), which is close to what the harness can resolve.Problem
Each compiled binding (
TypedBinding) adds its ownPropertyChangedhandler to everyINotifyPropertyChangedobject on its path. The bindings of an item template all observe the item. So when a CollectionView recycles a row, every binding removes its handler from the old item and adds one to the new item. Each add or remove copies the event's invocation list, so a rebind costs time and memory in proportion to the square of the number of bindings per object.The rows of the CollectionView scroll benchmark below have 13 compiled bindings. On Android, this churn (
TypedBindingSubscriptions.TryDetachandListener.Attach) accounts for 13% of the bytes that the UI thread allocates, andDelegate.RemoveImplfor 2.5% of its exclusive CPU time.Change
In
TypedBindingListeners.cs, the listeners of an object share one handler that notifies each of them:SharedSubscription: one handler that notifies its listeners in the order they joined. Later listeners of the object join it. The first listener keeps its own handler on that object, and shares from the next object it observes.A rebind of the benchmark's row now removes and adds one shared handler on the item, plus one for the nested
Ownerpath, instead of 14 each. A view model that 1,000 bindings observe has 2 handlers instead of 1,000.What's preserved
DispatchIfRequired). Only the thread that registered a shared subscription adds listeners to it. Any thread, including the finalizer thread, removes them, under a lock.Behavior changes
PropertyChangedhandler between the second and the third compiled binding of an object, the third and later bindings now run before that handler instead of after it.BindingsThatShareAHandlerAreNotifiedInOrderAtItsPositiondocuments this, and the existingGeneratedAliasedBroadcastPreservesEventOrderingpasses unchanged.PropertyChangedhandlers. Code that inspects the invocation list can observe this.Results
Measured against main bd40bc6, which this branch starts from. #40849 wasn't merged yet, and the combination wasn't measured. The device builds were made from d1d1fda0b6, which has the same tree as this PR's commit (
0d2644433f) with an earlier commit message. SDK 12.0.100-alpha.1.26523.103, CoreCLR.CollectionView scroll benchmark (source, pushed for reference and not part of this PR): a CollectionView of 1,000 items is scrolled programmatically on every frame. Its rows have 13 compiled bindings: strings, numbers, a DateTime, an enum, a Color, a nested path, StringFormats, converters and a two-way binding. Each build ran in 8 interleaved launches of 5 warm passes. The table shows medians of the launches, and the median paired difference with its 95% sign-test interval. Allocations, GC and JIT counts are exact per build.
pixel_6AVD with theandroid-35;google_apis;arm64-v8aimage, 4 cores, 4 GB), Android 15, on an Apple M5 Max.Host microbenchmarks:
TypedBindingDepthBenchmarkerandTypedBindingIntermediateBenchmarkerfromsrc/Core/tests/Benchmarks, and the benchmark branch'sCollectionViewRecyclingBenchmarker, which rebinds one row of the scroll benchmark's template without platform views. Release builds of main and this PR run in one process, each in its own AssemblyLoadContext, alternating every round. Each run has 60 rounds of 20 ms; the table shows the median of the per-round ratios, with its 95% sign-test interval in the first two rows. Apple M5 Max, macOS 26.7.3, load average 25–40. Two runs:Raw host results
baseandcandare medians of the rounds;pairedis the median of the per-round ratios, with its 95% interval.Tests
I merged this branch into main 18ccee2 locally (not pushed) and built each test project with
./.dotnet/dotnet build <project> -c Debug -p:MauiPlatforms= -p:IncludePreviousTfms=false:artifacts/bin/Controls.Core.UnitTests/Debug/net12.0/Microsoft.Maui.Controls.Core.UnitTests --progress off: 7,971 total, 7,941 passed, 30 skipped, 0 failed.artifacts/bin/Controls.Xaml.UnitTests/Debug/net12.0/Microsoft.Maui.Controls.Xaml.UnitTests --progress off: 1,655 total, 1,652 passed, 3 skipped, 0 failed.On this branch, before main moved on:
--filter-class '*TypedBinding*' --filter-class '*BindingUnitTests': 563 total, 561 passed, 2 skipped, 0 failed.dotnet format whitespace --verify-no-changesreports no changes in the changed files.New cases in
TypedBindingCompatibilityTests, each for both binding shapes (Legacy,Subscriber) unless noted:BindingsOfAnObjectShareOneHandler,ManyBindingsOfAnObjectShareItsHandler(one shape),BindingsOfObjectsObservedInTurnsAreEachNotifiedOnce.BindingsThatShareAHandlerAreNotifiedInOrderAtItsPosition.BindingRemovedDuringANotificationIsNotNotifiedByIt,BindingAddedDuringANotificationIsNotifiedByTheNextOne,BindingsMovedToAnotherObjectDuringANotificationAreNotNotifiedByIt.BindingDoesNotJoinASharedHandlerThatFailedToRegister.SharedHandlerIsRemovedAfterItsLastBindingIsCollected,NotificationRemovesTheListenersOfCollectedBindings,ObjectIsCollectableAfterItsBindingsStopObservingIt.SharedHandlerWorksWithOtherThreads(one shape).Not run: device tests, UI tests, SourceGen unit tests, the scroll benchmark on iOS and Windows, and CI.
Issues Fixed
None; there's no issue for this. Related:
Let AI finish this PR