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

Conversation

MichalStrehovsky
Copy link
Member

We don't need reflection metadata to answer this.

Change salvaged out of #118479, rt-sz will decide if we want it.

We don't need reflection metadata to answer this.

Change salvaged out of dotnet#118479, rt-sz will decide if we want it.
@Copilot Copilot AI review requested due to automatic review settings August 8, 2025 12:41
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes the AOT compilation process by avoiding the generation of unnecessary reflection metadata for simple type queries. The optimization specifically targets patterns like typeof(X).IsValueType and typeof(X).IsEnum where the answer can be determined at compile time without requiring full reflection metadata.

Key changes:

  • Adds pattern detection for typeof(X).IsValueType and typeof(X).IsEnum call sequences
  • Modifies helper ID assignment to use NecessaryTypeHandle instead of full metadata when these patterns are detected

Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

@MichalStrehovsky
Copy link
Member Author

The savings are borderline, but maybe worth it given the low complexity:

Size statistics

Pull request #118528

Project Size before Size after Difference
TodosApi-linux 24216856 24183768 -33088
TodosApi-windows 25530368 25500672 -29696
avalonia.app-linux 18799408 18799408 0
avalonia.app-windows 19328512 19328512 0
hello-linux 1364944 1364944 0
hello-minimal-linux 1094456 1094456 0
hello-minimal-windows 840192 840192 0
hello-windows 1138176 1138176 0
kestrel-minimal-linux 5201560 5180808 -20752
kestrel-minimal-windows 4746240 4727296 -18944
reflection-linux 1964096 1964096 0
reflection-windows 1823744 1823744 0
webapiaot-linux 9682544 9653568 -28976
webapiaot-windows 10245632 10218496 -27136
winrt-component-full-windows 6678528 6678528 0
winrt-component-minimal-windows 1708544 1708032 -512

@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

{
if (reader.HasNext
&& reader.ReadILOpcode() is ILOpcode.callvirt or ILOpcode.call
&& _methodIL.GetObject(reader.ReadILToken()) is MethodDesc { Name: "get_IsValueType" or "get_IsEnum"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add IsPrimitive too? It is used in some places in BCL.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see what rt-sz says. We can do this for several other properties but I wasn't sure how much of a coupling between the compiler and the BCL we are okay with in this area.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we wanted to avoid coupling between the compiler and the BCL, we would make this pattern must-expand in the JIT. It would reduce the coupling to JIT/scanner that exists in many places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test coverage for the (rare) situation when the JIT does not expand this pattern as an intrinsic?

@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants