Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix size regression from enum sorting #79845

Merged
merged 4 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ public abstract class ExecutionEnvironment
// Other
//==============================================================================================
public abstract FieldAccessor CreateLiteralFieldAccessor(object value, RuntimeTypeHandle fieldTypeHandle);
public abstract EnumInfo<TUnderlyingValue> GetEnumInfo<TUnderlyingValue>(RuntimeTypeHandle typeHandle)
where TUnderlyingValue : struct, INumber<TUnderlyingValue>;

public abstract void GetEnumInfo(RuntimeTypeHandle typeHandle, out string[] names, out object[] values, out bool isFlags);
public abstract IntPtr GetDynamicInvokeThunk(MethodInvoker invoker);

//==============================================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,40 @@ public sealed override EnumInfo<TUnderlyingValue> GetEnumInfo<TUnderlyingValue>(
if (info != null)
return info;

info = ReflectionCoreExecution.ExecutionDomain.ExecutionEnvironment.GetEnumInfo<TUnderlyingValue>(runtimeType.TypeHandle);
ReflectionCoreExecution.ExecutionDomain.ExecutionEnvironment.GetEnumInfo(runtimeType.TypeHandle, out string[] unsortedNames, out object[] unsortedValues, out bool isFlags);

// Call into IntrospectiveSort directly to avoid the Comparer<T>.Default codepath.
// That codepath would bring functionality to compare everything that was ever allocated in the program.
ArraySortHelper<object, string>.IntrospectiveSort(unsortedValues, unsortedNames, EnumUnderlyingTypeComparer.Instance);

// Only after we've sorted, create the underlying array.
var values = new TUnderlyingValue[unsortedValues.Length];
for (int i = 0; i < unsortedValues.Length; i++)
values[i] = (TUnderlyingValue)unsortedValues[i];

info = new EnumInfo<TUnderlyingValue>(RuntimeAugments.GetEnumUnderlyingType(runtimeType.TypeHandle), values, unsortedNames, isFlags);
runtimeType.GenericCache = info;
return info;
}

private class EnumUnderlyingTypeComparer : IComparer<object>
{
public readonly static EnumUnderlyingTypeComparer Instance = new EnumUnderlyingTypeComparer();
MichalStrehovsky marked this conversation as resolved.
Show resolved Hide resolved

public int Compare(object? x, object? y)
=> x switch
{
int i => i.CompareTo((int)y!),
uint ui => ui.CompareTo((uint)y!),
byte b => b.CompareTo((byte)y!),
ushort us => us.CompareTo((ushort)y!),
short s => s.CompareTo((short)y!),
sbyte sb => sb.CompareTo((sbyte)y!),
long l => l.CompareTo((long)y!),
_ => ((ulong)x!).CompareTo((ulong)y!),
};
}

public sealed override DynamicInvokeInfo GetDelegateDynamicInvokeInfo(Type type)
{
RuntimeTypeInfo runtimeType = type.CastToRuntimeTypeInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public sealed override FieldAccessor CreateLiteralFieldAccessor(object value, Ru
return new LiteralFieldAccessor(value, fieldTypeHandle);
}

public sealed override EnumInfo<TUnderlyingValue> GetEnumInfo<TUnderlyingValue>(RuntimeTypeHandle typeHandle)
public sealed override void GetEnumInfo(RuntimeTypeHandle typeHandle, out string[] names, out object[] values, out bool isFlags)
{
// Handle the weird case of an enum type nested under a generic type that makes the
// enum itself generic
Expand All @@ -141,7 +141,10 @@ public sealed override EnumInfo<TUnderlyingValue> GetEnumInfo<TUnderlyingValue>(
// If the type is reflection blocked, we pretend there are no enum values defined
if (ReflectionExecution.ExecutionEnvironment.IsReflectionBlocked(typeDefHandle))
{
return new EnumInfo<TUnderlyingValue>(RuntimeAugments.GetEnumUnderlyingType(typeHandle), Array.Empty<TUnderlyingValue>(), Array.Empty<string>(), false);
names = Array.Empty<string>();
values = Array.Empty<object>();
isFlags = false;
return;
}

QTypeDefinition qTypeDefinition;
Expand All @@ -152,15 +155,24 @@ public sealed override EnumInfo<TUnderlyingValue> GetEnumInfo<TUnderlyingValue>(

if (qTypeDefinition.IsNativeFormatMetadataBased)
{
return NativeFormatEnumInfo.Create<TUnderlyingValue>(typeHandle, qTypeDefinition.NativeFormatReader, qTypeDefinition.NativeFormatHandle);
NativeFormatEnumInfo.GetEnumValuesAndNames(
qTypeDefinition.NativeFormatReader,
qTypeDefinition.NativeFormatHandle,
out values,
out names,
out isFlags);
return;
}
#if ECMA_METADATA_SUPPORT
if (qTypeDefinition.IsEcmaFormatMetadataBased)
{
return EcmaFormatEnumInfo.Create<TUnderlyingValue>(typeHandle, qTypeDefinition.EcmaFormatReader, qTypeDefinition.EcmaFormatHandle);
}
#endif
return null;
names = Array.Empty<string>();
values = Array.Empty<object>();
isFlags = false;
return;
}

public override IntPtr GetDynamicInvokeThunk(MethodInvoker invoker)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace Internal.Reflection.Execution
{
static class NativeFormatEnumInfo
{
private static void GetEnumValuesAndNames(MetadataReader reader, TypeDefinitionHandle typeDefHandle,
out object[] sortedBoxedValues, out string[] sortedNames, out bool isFlags)
public static void GetEnumValuesAndNames(MetadataReader reader, TypeDefinitionHandle typeDefHandle,
out object[] unsortedBoxedValues, out string[] unsortedNames, out bool isFlags)
{
TypeDefinition typeDef = reader.GetTypeDefinition(typeDefHandle);

Expand All @@ -30,45 +30,27 @@ private static void GetEnumValuesAndNames(MetadataReader reader, TypeDefinitionH
}
}

var names = new string[staticFieldCount];
var boxedValues = new object[staticFieldCount]; // TODO: Avoid boxing the values
unsortedNames = new string[staticFieldCount];
unsortedBoxedValues = new object[staticFieldCount]; // TODO: Avoid boxing the values

int i = 0;
foreach (FieldHandle fieldHandle in typeDef.Fields)
{
Field field = fieldHandle.GetField(reader);
if (0 != (field.Flags & FieldAttributes.Static))
{
names[i] = field.Name.GetString(reader);
boxedValues[i] = field.DefaultValue.ParseConstantNumericValue(reader);
unsortedNames[i] = field.Name.GetString(reader);
unsortedBoxedValues[i] = field.DefaultValue.ParseConstantNumericValue(reader);
i++;
}
}

// Using object overload to avoid generic expansion for every underlying enum type
Array.Sort<object, string>(boxedValues, names);

sortedBoxedValues = boxedValues;
sortedNames = names;

isFlags = false;
foreach (CustomAttributeHandle cah in typeDef.CustomAttributes)
{
if (cah.IsCustomAttributeOfType(reader, "System", "FlagsAttribute"))
isFlags = true;
}
}

public static EnumInfo<TUnderlyingValue> Create<TUnderlyingValue>(RuntimeTypeHandle typeHandle, MetadataReader reader, TypeDefinitionHandle typeDefHandle)
where TUnderlyingValue : struct, INumber<TUnderlyingValue>
{
GetEnumValuesAndNames(reader, typeDefHandle, out object[] boxedValues, out string[] names, out bool isFlags);

var values = new TUnderlyingValue[boxedValues.Length];
for (int i = 0; i < boxedValues.Length; i++)
values[i] = (TUnderlyingValue)boxedValues[i];

return new EnumInfo<TUnderlyingValue>(RuntimeAugments.GetEnumUnderlyingType(typeHandle), values, names, isFlags);
}
}
}