Skip to content

Commit

Permalink
perf: Misc perf improvement to ResourceDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Apr 2, 2024
1 parent 73ee58c commit c3a2a3d
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions src/Uno.UI/UI/Xaml/ResourceDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,7 @@ internal bool TryGetValue(Type resourceKey, out object value, bool shouldCheckSy
=> TryGetValue(new ResourceKey(resourceKey), out value, shouldCheckSystem);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool TryGetValue(in ResourceKey resourceKey, out object value, bool shouldCheckSystem) =>
TryGetValue(resourceKey, ResourceKey.Empty, out value, shouldCheckSystem);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool TryGetValue(in ResourceKey resourceKey, ResourceKey activeTheme, out object value, bool shouldCheckSystem)
internal bool TryGetValue(in ResourceKey resourceKey, out object value, bool shouldCheckSystem)
{
if (_values.TryGetValue(resourceKey, out value))
{
Expand All @@ -226,12 +222,7 @@ private bool TryGetValue(in ResourceKey resourceKey, ResourceKey activeTheme, ou
return true;
}

if (activeTheme.IsEmpty)
{
activeTheme = Themes.Active;
}

if (GetFromTheme(resourceKey, activeTheme, out value))
if (GetFromTheme(resourceKey, GetActiveThemeDictionary(Themes.Active), out value))
{
return true;
}
Expand Down Expand Up @@ -434,25 +425,23 @@ private ResourceDictionary GetThemeDictionary(in ResourceKey theme)
return null;
}

private bool GetFromTheme(in ResourceKey resourceKey, in ResourceKey activeTheme, out object value)
private bool GetFromTheme(in ResourceKey resourceKey, ResourceDictionary activeThemeDictionary, out object value)
{
var dict = GetActiveThemeDictionary(activeTheme);

if (dict != null && dict.TryGetValue(resourceKey, out value, shouldCheckSystem: false))
if (activeThemeDictionary != null && activeThemeDictionary.TryGetValue(resourceKey, out value, shouldCheckSystem: false))
{
return true;
}

return GetFromThemeMerged(resourceKey, activeTheme, out value);
return GetFromThemeMerged(resourceKey, activeThemeDictionary, out value);
}

private bool GetFromThemeMerged(in ResourceKey resourceKey, in ResourceKey activeTheme, out object value)
private bool GetFromThemeMerged(in ResourceKey resourceKey, ResourceDictionary activeThemeDictionary, out object value)
{
var count = _mergedDictionaries.Count;

for (int i = count - 1; i >= 0; i--)
{
if (_mergedDictionaries[i].GetFromTheme(resourceKey, activeTheme, out value))
if (_mergedDictionaries[i].GetFromTheme(resourceKey, activeThemeDictionary, out value))
{
return true;
}
Expand Down

0 comments on commit c3a2a3d

Please sign in to comment.