Repository navigation
[Perf] Reduce allocations in inherited BindingContext propagation #34135
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 2 commits
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -363,7 +363,10 @@ public static void SetInheritedBindingContext(BindableObject bindable, object va | |||||||||||||||
| } | ||||||||||||||||
| else | ||||||||||||||||
| { | ||||||||||||||||
| bindable._inheritedContext = new WeakReference(value); | ||||||||||||||||
| if (bindable._inheritedContext is not null) | ||||||||||||||||
| bindable._inheritedContext.Target = value; | ||||||||||||||||
| else | ||||||||||||||||
| bindable._inheritedContext = new WeakReference(value); | ||||||||||||||||
| bindable.ApplyBindings(fromBindingContextChanged: true); | ||||||||||||||||
| bindable.OnBindingContextChanged(); | ||||||||||||||||
| } | ||||||||||||||||
|
|
@@ -692,11 +695,8 @@ private protected virtual void OnBindablePropertySet(BindableProperty property, | |||||||||||||||
|
|
||||||||||||||||
| void ApplyBindings(bool fromBindingContextChanged) | ||||||||||||||||
| { | ||||||||||||||||
| var prop = _properties.Values.ToArray(); | ||||||||||||||||
|
|
||||||||||||||||
| for (int i = 0, propLength = prop.Length; i < propLength; i++) | ||||||||||||||||
| foreach (var context in _properties.Values) | ||||||||||||||||
|
||||||||||||||||
| foreach (var context in _properties.Values) | |
| // Take a snapshot of the current property contexts so that re-entrant | |
| // modifications to the _properties collection (e.g., via propertyChanged | |
| // callbacks triggered during ApplyBinding) do not invalidate the enumerator. | |
| var propertyContexts = _properties.Values.ToArray(); | |
| foreach (var context in propertyContexts) |
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.
Iterating directly over
_properties.Valueshere is not safe:ApplyBindingultimately calls intoBindingBase.Apply/BindingExpression.ApplyCore, which can invokeSetValueCoreand property-changed callbacks. Those callbacks can add/remove bindable property contexts (modifying_properties) and will cause the dictionary enumerator to throwInvalidOperationExceptionmid-iteration. The previousToArray()snapshot avoided this; consider keeping a snapshot (potentially via pooled/rented array) or using a guarded pattern (e.g., retry or fallback snapshot when a modification is detected) so BindingContext propagation can’t crash on re-entrant property changes.