From 4bce59b8c745c144d5c05ea0701110c964238c9e Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Tue, 6 Apr 2021 21:05:39 -0400 Subject: [PATCH] perf: Improve name to DependencyProperty lookup --- ...dencyProperty.Dictionary.NameToProperty.cs | 55 +++++++++++++++++++ src/Uno.UI/UI/Xaml/DependencyProperty.cs | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/Uno.UI/UI/Xaml/DependencyProperty.Dictionary.NameToProperty.cs diff --git a/src/Uno.UI/UI/Xaml/DependencyProperty.Dictionary.NameToProperty.cs b/src/Uno.UI/UI/Xaml/DependencyProperty.Dictionary.NameToProperty.cs new file mode 100644 index 000000000000..ad2efd97e653 --- /dev/null +++ b/src/Uno.UI/UI/Xaml/DependencyProperty.Dictionary.NameToProperty.cs @@ -0,0 +1,55 @@ +#nullable enable + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Windows.UI.Xaml; +using Uno.Extensions; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using Uno; +using System.Threading; +using Uno.Collections; + +#if XAMARIN_ANDROID +using _View = Android.Views.View; +#elif XAMARIN_IOS_UNIFIED +using _View = UIKit.UIView; +#else +using _View = Windows.UI.Xaml.UIElement; +#endif + +namespace Windows.UI.Xaml +{ + public sealed partial class DependencyProperty + { + private class NameToPropertyDictionary + { + private readonly HashtableEx _entries = new HashtableEx(PropertyCacheEntry.DefaultComparer); + + internal bool TryGetValue(PropertyCacheEntry key, out DependencyProperty? result) + { + if (_entries.TryGetValue(key, out var value)) + { + result = (DependencyProperty)value!; + + return true; + } + + result = null; + return false; + } + + internal void Add(PropertyCacheEntry key, DependencyProperty dependencyProperty) + => _entries.Add(key, dependencyProperty); + + internal void Remove(PropertyCacheEntry propertyCacheEntry) + => _entries.Remove(propertyCacheEntry); + + internal int Count => _entries.Count; + + internal void Clear() => _entries.Clear(); + } + } +} diff --git a/src/Uno.UI/UI/Xaml/DependencyProperty.cs b/src/Uno.UI/UI/Xaml/DependencyProperty.cs index f600f6e0af94..385e2428bb96 100644 --- a/src/Uno.UI/UI/Xaml/DependencyProperty.cs +++ b/src/Uno.UI/UI/Xaml/DependencyProperty.cs @@ -30,7 +30,7 @@ private static Dictionary> _registr = new Dictionary>(Uno.Core.Comparison.FastTypeComparer.Default); private readonly static Dictionary _getPropertiesForType = new Dictionary(Uno.Core.Comparison.FastTypeComparer.Default); - private readonly static Dictionary _getPropertyCache = new Dictionary(PropertyCacheEntry.DefaultComparer); + private readonly static NameToPropertyDictionary _getPropertyCache = new NameToPropertyDictionary(); private readonly static Dictionary, DependencyProperty[]> _getFrameworkPropertiesForType = new Dictionary, DependencyProperty[]>(CachedTuple.Comparer); private readonly static Dictionary _getDependencyObjectPropertiesForType = new Dictionary(Uno.Core.Comparison.FastTypeComparer.Default);