-
Notifications
You must be signed in to change notification settings - Fork 2k
[Perf] Cache PropertyChanged/ChangingEventArgs on BindableProperty #34136
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
base: net11.0
Are you sure you want to change the base?
Changes from all commits
19945fd
eba1e07
896c058
e3e616a
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| #nullable disable | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
| using System.ComponentModel; | ||
| using System.Diagnostics; | ||
|
|
@@ -252,6 +253,15 @@ public sealed class BindableProperty | |
|
|
||
| internal ValidateValueDelegate ValidateValue { get; private set; } | ||
|
|
||
| private static readonly ConcurrentDictionary<string, PropertyChangedEventArgs> s_changedArgsCache = new(); | ||
|
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. These caches are |
||
| private static readonly ConcurrentDictionary<string, PropertyChangingEventArgs> s_changingArgsCache = new(); | ||
|
|
||
| internal static PropertyChangedEventArgs GetCachedPropertyChangedEventArgs(string propertyName) | ||
| => s_changedArgsCache.GetOrAdd(propertyName, static name => new PropertyChangedEventArgs(name)); | ||
|
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.
|
||
|
|
||
| internal static PropertyChangingEventArgs GetCachedPropertyChangingEventArgs(string propertyName) | ||
|
Collaborator
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. [major] Memory Leak Prevention / Performance — These static caches are populated through the protected
Collaborator
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.
[major] Memory Leak Prevention — These are process-lifetime |
||
| => s_changingArgsCache.GetOrAdd(propertyName, static name => new PropertyChangingEventArgs(name)); | ||
|
|
||
| // Properties that this property depends on - when getting this property's value, | ||
|
Collaborator
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] Performance-Critical Path — This replaces a small gen0 allocation on the property-change path with a string hash plus |
||
| // if the dependency has a pending binding, return the default value instead. | ||
|
Collaborator
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.
[critical] Logic and Correctness — |
||
| // This is used to fix timing issues where one property binding resolves before another. | ||
|
|
||
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.
I believe we should set up a max value here. For Maui properties, this can be fixed size and will not cause any memory pressure, but for apps, the large ones mainly, with custom controls and different property names, it can cause a memory pressure. In the wild, I can say that people choose funny names for their properties; for example, instead of choosing
Textas a property name, it can be Title, TitleText, etc., causing this collection to grow and may cause a memory pressure at some point.