-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Drop size of hello world to 745 kB #118718
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
Conversation
(With `StackTraceSupport=false`, `OptimizationPreference=Size`, `UseSystemResourceKeys=true`) I don't know if we want it in this form, but it is tempting. The thing that makes the difference is that we are no longer boxing enums and we can get rid of the super expensive enum stringification logic. Not boxing enums also means that dotnet#118640 can fully kick in because enums are the last remaining place that looks at custom attributes (looking for `FlagsAttribute`).
There was a problem hiding this 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 aims to reduce the size of hello world applications to 745 kB by eliminating enum boxing and expensive enum stringification logic. The changes focus on optimizing enum handling in the NativeAOT runtime to enable better size optimizations.
Key changes:
- Avoids enum boxing by using direct value comparisons and explicit casts
- Replaces enum ToString() calls with hardcoded string switch expressions
- Refactors methods to bypass enum validation when internal consistency is guaranteed
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
CultureData.cs | Replaces enum array initialization with cast from ushort array to avoid boxing |
Thread.NativeAot.Windows.cs | Changes method visibility and replaces enum ToString() with switch expression |
GC.NativeAot.cs | Refactors to bypass enum validation in internal calls |
MethodTable.Runtime.cs | Optimizes enum detection logic using direct type comparison |
StartupCodeHelpers.Extensions.cs | Uses internal unchecked method to avoid enum validation overhead |
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs
Outdated
Show resolved
Hide resolved
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/MethodTable.Runtime.cs
Show resolved
Hide resolved
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas |
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs
Outdated
Show resolved
Hide resolved
...te.CoreLib/src/Internal/Runtime/CompilerHelpers/StartupCode/StartupCodeHelpers.Extensions.cs
Outdated
Show resolved
Hide resolved
This looks reasonable to me. The unnatural patterns need comments to make it clear why they are needed. |
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Threading/Thread.NativeAot.Windows.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs
Show resolved
Hide resolved
…lysis A couple times in the past I needed a way to say "if X is not part of the program, eliminate the entire basic block". We can do this for allocated types (this is how branches under `is` checks elimination works), but we can't do this for more general "characteristics". This introduces a mechanism where AOT compiler and CoreLib (or System.Private.* universe in general) can define whole program tags such as "whole program has X in it" and CoreLib can condition code on the presence of this tag. This is easier shown than described, so I extracted the first use of this into a separate commit. In this commit, we eliminate code that tries looking for `StackTraceHiddenAttribute` if we know the whole program has no `StackTraceHiddenAttribute` in it. With this code eliminated, dotnet#118640 can then eliminate all custom attributes on methods, which in turn plays into dotnet#118718 and we can eliminate enum boxing even when StackTraceSupport is not set to false (right now dotnet#118718 really needs the StackTraceSupport=false to get rid of boxed enums; we get more boxed enums from method attributes). We have a new node that represents the characteristic. The node can be dropped into the graph wherever needed. ILScanner then uses this to condition parts of the method body on this characteristic node. We need similar logic in the substitution IL provider because we need to guarantee that RyuJIT is not going to see basic blocks we didn't scan. So we treat it as a substitution during codegen phase too.
…lysis (#118769) A couple times in the past I needed a way to say "if X is not part of the program, eliminate the entire basic block". We can do this for allocated types (this is how branches under `is` checks elimination works), but we can't do this for more general "characteristics". This introduces a mechanism where AOT compiler and CoreLib (or System.Private.* universe in general) can define whole program tags such as "whole program has X in it" and CoreLib can condition code on the presence of this tag. This is easier shown than described, so I extracted the first use of this into a separate commit. In this commit, we eliminate code that tries looking for `StackTraceHiddenAttribute` if we know the whole program has no `StackTraceHiddenAttribute` in it. With this code eliminated, #118640 can then eliminate all custom attributes on methods, which in turn plays into #118718 and we can eliminate enum boxing even when StackTraceSupport is not set to false (right now #118718 really needs the StackTraceSupport=false to get rid of boxed enums; we get more boxed enums from method attributes). We have a new node that represents the characteristic. The node can be dropped into the graph wherever needed. ILScanner then uses this to condition parts of the method body on this characteristic node. We need similar logic in the substitution IL provider because we need to guarantee that RyuJIT is not going to see basic blocks we didn't scan. So we treat it as a substitution during codegen phase too.
(With
StackTraceSupport=false
,OptimizationPreference=Size
,UseSystemResourceKeys=true
)I don't know if we want it in this form, but it is tempting. The thing that makes the difference is that we are no longer boxing enums and we can get rid of the super expensive enum stringification logic.
Not boxing enums also means that #118640 can fully kick in because enums are the last remaining place that looks at custom attributes (looking for
FlagsAttribute
).Cc @dotnet/ilc-contrib