Skip to content

Commit 3b40a9c

Browse files
committed
perf: Cache self style key to avoid Type.ToString invocations
1 parent bb71491 commit 3b40a9c

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/Uno.UI/UI/Xaml/DependencyObjectExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ internal static void StoreDisableHardReferences(this IDependencyObjectStoreProvi
9090
/// Gets the implicit style for the current object
9191
/// </summary>
9292
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
93-
internal static Style StoreGetImplicitStyle(this IDependencyObjectStoreProvider provider)
94-
=> provider.Store.GetImplicitStyle();
93+
internal static Style StoreGetImplicitStyle(this IDependencyObjectStoreProvider provider, in SpecializedResourceDictionary.ResourceKey styleKey)
94+
=> provider.Store.GetImplicitStyle(styleKey);
9595

9696
internal static IEnumerable<object> GetParents(this object dependencyObject)
9797
{

src/Uno.UI/UI/Xaml/DependencyObjectStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,11 +1246,11 @@ private IEnumerable<ResourceDictionary> GetResourceDictionaries(bool includeAppR
12461246
/// <summary>
12471247
/// Retrieve the implicit Style for <see cref="ActualInstance"/> by walking the visual tree.
12481248
/// </summary>
1249-
internal Style? GetImplicitStyle()
1249+
internal Style? GetImplicitStyle(in SpecializedResourceDictionary.ResourceKey styleKey)
12501250
{
12511251
foreach (var dict in GetResourceDictionaries(includeAppResources: true))
12521252
{
1253-
if (dict.TryGetValue(_originalObjectType, out var style, shouldCheckSystem: false))
1253+
if (dict.TryGetValue(styleKey, out var style, shouldCheckSystem: false))
12541254
{
12551255
return style as Style;
12561256
}

src/Uno.UI/UI/Xaml/FrameworkElement.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,17 @@ public static class TraceProvider
6060

6161
private bool _defaultStyleApplied = false;
6262
private protected bool IsDefaultStyleApplied => _defaultStyleApplied;
63+
6364
/// <summary>
6465
/// The current user-determined 'active Style'. This will either be the explicitly-set Style, if there is one, or otherwise the resolved implicit Style (either in the view hierarchy or in Application.Resources).
6566
/// </summary>
6667
private Style _activeStyle = null;
6768

69+
/// <summary>
70+
/// Cache for the current type key for faster implicit style lookup
71+
/// </summary>
72+
private SpecializedResourceDictionary.ResourceKey _thisTypeResourceKey;
73+
6874
/// <summary>
6975
/// Sets whether constraint-based optimizations are used to limit redrawing of the entire visual tree on Android. This can be
7076
/// globally set to false if it is causing visual errors (eg views not updating properly). Note: this can still be overridden by
@@ -383,7 +389,20 @@ private void UpdateActiveStyle()
383389
}
384390
}
385391

386-
private Style ResolveImplicitStyle() => this.StoreGetImplicitStyle();
392+
private SpecializedResourceDictionary.ResourceKey ThisTypeResourceKey
393+
{
394+
get
395+
{
396+
if(_thisTypeResourceKey.IsEmpty)
397+
{
398+
_thisTypeResourceKey = this.GetType();
399+
}
400+
401+
return _thisTypeResourceKey;
402+
}
403+
}
404+
405+
private Style ResolveImplicitStyle() => this.StoreGetImplicitStyle(ThisTypeResourceKey);
387406

388407
#region Requested theme dependency property
389408

0 commit comments

Comments
 (0)