Skip to content

Optimize out metadata for typeof(X).IsValueType and .IsEnum #118528

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -959,6 +959,25 @@ private void ImportLdToken(int token)
}
}

// We might also be able to optimize this if this is something like typeof(Foo).IsValueType
// that doesn't need reflection metadata for the answer.
if (helperId != ReadyToRunHelperId.NecessaryTypeHandle)
{
reader = new ILReader(_ilBytes, _currentOffset);
if (reader.HasNext
&& reader.ReadILOpcode() == ILOpcode.call
&& IsTypeGetTypeFromHandle((MethodDesc)_methodIL.GetObject(reader.ReadILToken())))
{
if (reader.HasNext
&& reader.ReadILOpcode() is ILOpcode.callvirt or ILOpcode.call
&& _methodIL.GetObject(reader.ReadILToken()) is MethodDesc
{ Name: "get_IsValueType" or "get_IsEnum" or "get_IsPrimitive" })
{
helperId = ReadyToRunHelperId.NecessaryTypeHandle;
}
}
}

_factory.MetadataManager.GetDependenciesDueToAccess(ref _dependencies, _factory, _methodIL, (TypeDesc)_canonMethodIL.GetObject(token));

_dependencies.Add(GetHelperEntrypoint(ReadyToRunHelper.GetRuntimeTypeHandle), "ldtoken");
Expand Down
Loading