Skip to content

Commit

Permalink
perf: Fix regression in SpecializedResourceDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
ebariche committed Feb 25, 2022
1 parent e9c593e commit 44a49f9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

using ResourceKey = Windows.UI.Xaml.SpecializedResourceDictionary.ResourceKey;

namespace Uno.UI.Tests.Windows_UI_Xaml
{
[TestClass]
public class Given_SpecializedResourceDictionary
{
[TestInitialize]
public void Init()
{
UnitTestsApp.App.EnsureApplication();
}

[TestMethod]
public void When_ResourceKey_HashCode_Equal()
{
var s1 = "StringKey";
var s2 = "StringKey";

var t1 = typeof(string);
var t2 = typeof(string);

var k1 = new ResourceKey(s1);
var k2 = new ResourceKey(s2);
var k3 = new ResourceKey(t1);
var k4 = new ResourceKey(t2);

Assert.AreEqual(k1.HashCode, k2.HashCode);
Assert.AreEqual(k3.HashCode, k4.HashCode);
}

[TestMethod]
public void When_ResourceKey_HashCode_NotEqual()
{
var s1 = "StringKey";
var s2 = "ByteKey";

var t1 = typeof(string);
var t2 = typeof(byte);

var k1 = new ResourceKey(s1);
var k2 = new ResourceKey(s2);
var k3 = new ResourceKey(t1);
var k4 = new ResourceKey(t2);

Assert.AreNotEqual(k1.HashCode, k2.HashCode);
Assert.AreNotEqual(k3.HashCode, k4.HashCode);
}
}
}
18 changes: 9 additions & 9 deletions src/Uno.UI/UI/Xaml/SpecializedResourceDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public ResourceKey(object key)
{
Key = s;
TypeKey = null;
HashCode = (uint)(s.GetHashCode() ^ TypeKey?.GetHashCode() ?? 0);
HashCode = (uint)s.GetHashCode();
}
else if (key is Type t)
{
Key = t.ToString();
Key = t.FullName;
TypeKey = t;
HashCode = (uint)(t.GetHashCode() ^ TypeKey?.GetHashCode() ?? 0);
HashCode = (uint)t.GetHashCode();
}
else if (key is ResourceKey)
{
Expand All @@ -73,7 +73,7 @@ public ResourceKey(object key)
{
Key = key.ToString();
TypeKey = null;
HashCode = (uint)(key.GetHashCode() ^ TypeKey?.GetHashCode() ?? 0);
HashCode = (uint)key.GetHashCode();
}
}

Expand All @@ -85,7 +85,7 @@ public ResourceKey(string key)
{
Key = key;
TypeKey = null;
HashCode = (uint)(key.GetHashCode() ^ TypeKey?.GetHashCode() ?? 0);
HashCode = (uint)key.GetHashCode();
}

/// <summary>
Expand All @@ -94,15 +94,15 @@ public ResourceKey(string key)
/// <param name="key">A string typed key</param>
public ResourceKey(Type key)
{
Key = key.ToString();
Key = key.FullName;
TypeKey = key;
HashCode = (uint)(key.GetHashCode() ^ TypeKey?.GetHashCode() ?? 0);
HashCode = (uint)key.GetHashCode();
}

/// <summary>
/// Compares this instance with another ResourceKey instance
/// </summary>
public bool Equals(ResourceKey other)
public bool Equals(in ResourceKey other)
=> TypeKey == other.TypeKey && Key == other.Key;


Expand Down Expand Up @@ -300,7 +300,7 @@ private ref object FindValue(in ResourceKey key)
{
Debug.Assert(_entries != null, "expected entries to be != null");

uint hashCode = (uint)key.HashCode;
uint hashCode = key.HashCode;
int i = GetBucket(hashCode);
Entry[] entries = _entries;
uint length = (uint)entries.Length;
Expand Down

0 comments on commit 44a49f9

Please sign in to comment.