Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d0c3b35

Browse files
committed
Use EqualityComparer<TKey>.Default.Equals intrinsic
1 parent 473e5da commit d0c3b35

File tree

1 file changed

+3
-69
lines changed

1 file changed

+3
-69
lines changed

src/mscorlib/src/System/Collections/Generic/Dictionary.cs

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,7 @@ private ref Entry FindEntry<TComparer>(TKey key, out bool found, IEqualityCompar
400400
{
401401
if (typeof(TComparer) == typeof(DefaultComparer))
402402
{
403-
// Change to EqualityComparer<T>.Default when Jit recognises intrinsic https://github.com/dotnet/coreclr/pull/14125
404-
if (default(TKey) == null ? entry.key.Equals(key) : StructKeyEquals(entry.key, key))
403+
if (EqualityComparer<TKey>.Default.Equals(entry.key, key))
405404
{
406405
return ref entry;
407406
}
@@ -502,8 +501,7 @@ private bool TryInsert<TInsertionBehavior, TComparer>(TKey key, TValue value, IE
502501
bool keysEqual = false;
503502
if (typeof(TComparer) == typeof(DefaultComparer))
504503
{
505-
// Change to EqualityComparer<T>.Default when Jit recognises intrinsic https://github.com/dotnet/coreclr/pull/14125
506-
keysEqual = default(TKey) == null ? candidateEntry.key.Equals(key) : StructKeyEquals(candidateEntry.key, key);
504+
keysEqual = EqualityComparer<TKey>.Default.Equals(candidateEntry.key, key);
507505
}
508506
else if (typeof(TComparer) == typeof(CustomComparer))
509507
{
@@ -784,8 +782,7 @@ private ref Entry Remove<TComparer>(TKey key, out bool success, IEqualityCompare
784782
bool keysEqual = false;
785783
if (typeof(TComparer) == typeof(DefaultComparer))
786784
{
787-
// Change to EqualityComparer<T>.Default when Jit recognises intrinsic https://github.com/dotnet/coreclr/pull/14125
788-
keysEqual = default(TKey) == null ? entry.key.Equals(key) : StructKeyEquals(entry.key, key);
785+
keysEqual = EqualityComparer<TKey>.Default.Equals(entry.key, key);
789786
}
790787
else if (typeof(TComparer) == typeof(CustomComparer))
791788
{
@@ -838,69 +835,6 @@ public bool TryGetValue(TKey key, out TValue value)
838835

839836
public bool TryAdd(TKey key, TValue value) => TryInsert<RejectIfExisting>(key, value);
840837

841-
// Replace with EqualityComparer<T>.Default when Jit recognises intrinsic https://github.com/dotnet/coreclr/pull/14125
842-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
843-
private bool StructKeyEquals(TKey key0, TKey key1)
844-
{
845-
Debug.Assert(default(TKey) != null);
846-
847-
// Specialize equality for each of the BCL ValueType primitives
848-
// cast via object to the type and use them directly
849-
// the Jit will elide the boxing.
850-
if (typeof(TKey) == typeof(byte))
851-
{
852-
return (byte)(object)key0 == (byte)(object)key1;
853-
}
854-
else if (typeof(TKey) == typeof(sbyte))
855-
{
856-
return (sbyte)(object)key0 == (sbyte)(object)key1;
857-
}
858-
else if (typeof(TKey) == typeof(ushort))
859-
{
860-
return (ushort)(object)key0 == (ushort)(object)key1;
861-
}
862-
else if (typeof(TKey) == typeof(short))
863-
{
864-
return (short)(object)key0 == (short)(object)key1;
865-
}
866-
else if (typeof(TKey) == typeof(char))
867-
{
868-
return (char)(object)key0 == (char)(object)key1;
869-
}
870-
else if (typeof(TKey) == typeof(uint))
871-
{
872-
return (uint)(object)key0 == (uint)(object)key1;
873-
}
874-
else if (typeof(TKey) == typeof(int))
875-
{
876-
return (int)(object)key0 == (int)(object)key1;
877-
}
878-
else if (typeof(TKey) == typeof(ulong))
879-
{
880-
return (ulong)(object)key0 == (ulong)(object)key1;
881-
}
882-
else if (typeof(TKey) == typeof(long))
883-
{
884-
return (long)(object)key0 == (long)(object)key1;
885-
}
886-
else if (typeof(TKey) == typeof(IntPtr))
887-
{
888-
return (IntPtr)(object)key0 == (IntPtr)(object)key1;
889-
}
890-
else if (typeof(TKey) == typeof(UIntPtr))
891-
{
892-
return (UIntPtr)(object)key0 == (UIntPtr)(object)key1;
893-
}
894-
else if (typeof(TKey) == typeof(Guid))
895-
{
896-
return (Guid)(object)key0 == (Guid)(object)key1;
897-
}
898-
else
899-
{
900-
return EqualityComparer<TKey>.Default.Equals(key0, key1);
901-
}
902-
}
903-
904838
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => false;
905839

906840
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int index) => CopyTo(array, index);

0 commit comments

Comments
 (0)