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

Reduce RuntimeType.MakeGenericType overheads #45137

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -469,6 +469,17 @@ internal Type[] GetInstantiationPublic()
[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
private static extern void Instantiate(QCallTypeHandle handle, IntPtr* pInst, int numGenericArgs, ObjectHandleOnStack type);

internal RuntimeType Instantiate(RuntimeType inst)
{
IntPtr ptr = inst.TypeHandle.Value;

RuntimeType? type = null;
RuntimeTypeHandle nativeHandle = GetNativeHandle();
Instantiate(new QCallTypeHandle(ref nativeHandle), &ptr, 1, ObjectHandleOnStack.Create(ref type));
GC.KeepAlive(inst);
return type!;
}

internal RuntimeType Instantiate(Type[]? inst)
{
// defensive copy to be sure array is not mutated from the outside during processing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3238,15 +3238,29 @@ public override Type MakeGenericType(Type[] instantiation)
if (instantiation == null)
throw new ArgumentNullException(nameof(instantiation));

RuntimeType[] instantiationRuntimeType = new RuntimeType[instantiation.Length];

if (!IsGenericTypeDefinition)
throw new InvalidOperationException(
SR.Format(SR.Arg_NotGenericTypeDefinition, this));
throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this));

if (GetGenericArguments().Length != instantiation.Length)
RuntimeType[] genericParameters = GetGenericArgumentsInternal();
if (genericParameters.Length != instantiation.Length)
throw new ArgumentException(SR.Argument_GenericArgsCount, nameof(instantiation));

if (instantiation.Length == 1 && instantiation[0] is RuntimeType rt)
{
ThrowIfTypeNeverValidGenericArgument(rt);
try
{
return new RuntimeTypeHandle(this).Instantiate(rt);
}
catch (TypeLoadException e)
{
ValidateGenericArguments(this, new[] { rt }, e);
throw;
}
}

RuntimeType[] instantiationRuntimeType = new RuntimeType[instantiation.Length];

bool foundSigType = false;
bool foundNonRuntimeType = false;
for (int i = 0; i < instantiation.Length; i++)
Expand Down Expand Up @@ -3277,8 +3291,6 @@ public override Type MakeGenericType(Type[] instantiation)
return System.Reflection.Emit.TypeBuilderInstantiation.MakeGenericType(this, (Type[])(instantiation.Clone()));
}

RuntimeType[] genericParameters = GetGenericArgumentsInternal();

SanityCheckGenericArguments(instantiationRuntimeType, genericParameters);

Type ret;
Expand Down