Repository navigation
Description
What would you like to be added? 💡
Subscribe {Binding} to an INotifyPropertyChanged source through one shared handler per source, instead of one handler per binding path segment. The shared handler would forward to a list of bindings held weakly, and prune the dead entries itself.
Today, BindingPath.SubscribeToNotifyPropertyChanged adds a handler to the source for every binding path segment that reads from it. Since #26115, each of those handlers is removed once its binding is collected (a finalizer queues source.PropertyChanged -= handler to the UI thread). The problem is the cost of that removal: every -= on a multicast delegate copies the whole invocation list, and an event offers no bulk removal. So removing n dead handlers from one source costs O(n²) in time and allocations. Adding them stays cheap, since Delegate.Combine appends in place.
A per-source fan-out would turn each removal into maintenance of a list we own (no delegate copies), and would also cut the per-binding allocations (one delegate per binding today).
Why is this needed? 🤔
Measured on Skia desktop (Win32) while reviewing #26115, with a view model kept for the session and a page with 3000 bindings to it:
- Navigating to the page 20 times, with a full GC between visits: the queued removals cost about 72 ms of UI-thread time per visit (in batches of at most 3 ms, so the UI stays responsive), plus about 65 MB of garbage per visit from the copied invocation lists.
- Worst case, 20 such pages collected before any removal runs (90,000 handlers pending on one view model): draining them takes about 60 s of UI-thread CPU.
The worst case is plausible on WebAssembly, where pages promoted to gen2 wait for a full collection, so several of them can be finalized at once.
Before #26115 the handlers were never removed at all (an unbounded leak, and every raise iterated them all), so #26115 is a net improvement; this issue is about the remaining cost.
For which platforms 📱💻🖥️
All platforms 🌍
Anything else we need to know? 💬
- Keep
Disposedeterministic: recycled containers dispose their bindings on everyDataContextchange, and that path is hot (ItemsRepeater,ListView). - The table holding the per-source fan-out (e.g. a
ConditionalWeakTablekeyed by the source) must not keep the source alive, and the source must not keep the bindings alive. - Raise semantics: forward over a snapshot, as the source may raise re-entrantly, and a binding may be added or removed during a raise.
- The same pattern could later serve the indexer-path
CollectionChangedsubscription inBindingPath. - Regression coverage:
Given_BindingStaleHandlers,Given_BindingMemoryLeak, theWindows_UI_Xaml_Datasuites, plus the before/after measurement above.
Activity