- Make EnumEqualityComparer sealed. This helps with JIT optimizations.
- Delete SByteEnumEqualityComparer and ShortEnumEqualityComparer because of they are not needed anymore. The optimization that they were introduced for is done by the JIT now.
- Deleted ForwardedFrom attributes on types that do not need them
// since we want to serialize as ObjectComparer for
// back-compat reasons (see below).
[Serializable]
- [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
internal sealed class Int32EnumComparer<T> : Comparer<T>, ISerializable where T : struct
{
public Int32EnumComparer()
}
[Serializable]
- [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
internal sealed class UInt32EnumComparer<T> : Comparer<T>, ISerializable where T : struct
{
public UInt32EnumComparer()
}
[Serializable]
- [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
internal sealed class Int64EnumComparer<T> : Comparer<T>, ISerializable where T : struct
{
public Int64EnumComparer()
}
[Serializable]
- [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
internal sealed class UInt64EnumComparer<T> : Comparer<T>, ISerializable where T : struct
{
public UInt64EnumComparer()
TypeCode underlyingTypeCode = Type.GetTypeCode(Enum.GetUnderlyingType(enumType));
// Depending on the enum type, we need to special case the comparers so that we avoid boxing.
- // Note: We have different comparers for short and sbyte, since for those types GetHashCode does not simply return the value.
- // We need to preserve what they would return.
switch (underlyingTypeCode)
{
- case TypeCode.Int16:
- return RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(ShortEnumEqualityComparer<short>), enumType);
- case TypeCode.SByte:
- return RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(SByteEnumEqualityComparer<sbyte>), enumType);
case TypeCode.Int32:
case TypeCode.UInt32:
+ case TypeCode.SByte:
case TypeCode.Byte:
+ case TypeCode.Int16:
case TypeCode.UInt16:
return RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(EnumEqualityComparer<int>), enumType);
case TypeCode.Int64:
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
- internal class EnumEqualityComparer<T> : EqualityComparer<T>, ISerializable where T : struct
+ internal sealed class EnumEqualityComparer<T> : EqualityComparer<T>, ISerializable where T : struct
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(T x, T y)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode(T obj)
{
- int x_final = System.Runtime.CompilerServices.JitHelpers.UnsafeEnumCast(obj);
- return x_final.GetHashCode();
+ return obj.GetHashCode();
}
public EnumEqualityComparer() { }
}
[Serializable]
- [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
- internal sealed class SByteEnumEqualityComparer<T> : EnumEqualityComparer<T> where T : struct
- {
- public SByteEnumEqualityComparer() { }
-
- // This is used by the serialization engine.
- public SByteEnumEqualityComparer(SerializationInfo information, StreamingContext context) { }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override int GetHashCode(T obj)
- {
- int x_final = System.Runtime.CompilerServices.JitHelpers.UnsafeEnumCast(obj);
- return ((sbyte)x_final).GetHashCode();
- }
- }
-
- [Serializable]
- [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
- internal sealed class ShortEnumEqualityComparer<T> : EnumEqualityComparer<T>, ISerializable where T : struct
- {
- public ShortEnumEqualityComparer() { }
-
- // This is used by the serialization engine.
- public ShortEnumEqualityComparer(SerializationInfo information, StreamingContext context) { }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override int GetHashCode(T obj)
- {
- int x_final = System.Runtime.CompilerServices.JitHelpers.UnsafeEnumCast(obj);
- return ((short)x_final).GetHashCode();
- }
- }
-
- [Serializable]
- [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
internal sealed class LongEnumEqualityComparer<T> : EqualityComparer<T>, ISerializable where T : struct
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode(T obj)
{
- long x_final = System.Runtime.CompilerServices.JitHelpers.UnsafeEnumCastLong(obj);
- return x_final.GetHashCode();
+ return obj.GetHashCode();
}
// Equals method for the comparer itself.
if (et == ELEMENT_TYPE_I4 ||
et == ELEMENT_TYPE_U4 ||
et == ELEMENT_TYPE_U2 ||
- et == ELEMENT_TYPE_U1)
+ et == ELEMENT_TYPE_I2 ||
+ et == ELEMENT_TYPE_U1 ||
+ et == ELEMENT_TYPE_I1)
{
ss.Set(W("System.Collections.Generic.EnumEqualityComparer`1"));
return;
}
- else if (et == ELEMENT_TYPE_I2)
- {
- ss.Set(W("System.Collections.Generic.ShortEnumEqualityComparer`1"));
- return;
- }
- else if (et == ELEMENT_TYPE_I1)
- {
- ss.Set(W("System.Collections.Generic.SByteEnumEqualityComparer`1"));
- return;
- }
else if (et == ELEMENT_TYPE_I8 ||
et == ELEMENT_TYPE_U8)
{
switch(normType)
{
case ELEMENT_TYPE_I1:
- {
- targetClass = MscorlibBinder::GetClass(CLASS__SBYTE_ENUM_EQUALITYCOMPARER);
- break;
- }
-
case ELEMENT_TYPE_I2:
- {
- targetClass = MscorlibBinder::GetClass(CLASS__SHORT_ENUM_EQUALITYCOMPARER);
- break;
- }
-
case ELEMENT_TYPE_U1:
case ELEMENT_TYPE_U2:
case ELEMENT_TYPE_I4:
// Classes referenced in EqualityComparer<T>.Default optimization
DEFINE_CLASS(BYTE_EQUALITYCOMPARER, CollectionsGeneric, ByteEqualityComparer)
-DEFINE_CLASS(SHORT_ENUM_EQUALITYCOMPARER, CollectionsGeneric, ShortEnumEqualityComparer`1)
-DEFINE_CLASS(SBYTE_ENUM_EQUALITYCOMPARER, CollectionsGeneric, SByteEnumEqualityComparer`1)
DEFINE_CLASS(ENUM_EQUALITYCOMPARER, CollectionsGeneric, EnumEqualityComparer`1)
DEFINE_CLASS(LONG_ENUM_EQUALITYCOMPARER, CollectionsGeneric, LongEnumEqualityComparer`1)
DEFINE_CLASS(NULLABLE_EQUALITYCOMPARER, CollectionsGeneric, NullableEqualityComparer`1)