Drop size of hello world to 745 kB#118718
Merged
jkotas merged 3 commits intodotnet:mainfrom Aug 15, 2025
Merged
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`).
Contributor
There was a problem hiding this comment.
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
Contributor
|
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas |
This was referenced Aug 14, 2025
Sergio0694
reviewed
Aug 14, 2025
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs
Outdated
Show resolved
Hide resolved
jkotas
reviewed
Aug 14, 2025
...te.CoreLib/src/Internal/Runtime/CompilerHelpers/StartupCode/StartupCodeHelpers.Extensions.cs
Outdated
Show resolved
Hide resolved
Member
|
This looks reasonable to me. The unnatural patterns need comments to make it clear why they are needed. |
jkotas
reviewed
Aug 14, 2025
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Threading/Thread.NativeAot.Windows.cs
Show resolved
Hide resolved
jkotas
reviewed
Aug 14, 2025
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs
Show resolved
Hide resolved
This was referenced Aug 15, 2025
jkotas
reviewed
Aug 15, 2025
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs
Outdated
Show resolved
Hide resolved
jkotas
reviewed
Aug 15, 2025
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs
Outdated
Show resolved
Hide resolved
jkotas
reviewed
Aug 15, 2025
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs
Show resolved
Hide resolved
MichalStrehovsky
added a commit
to MichalStrehovsky/runtime
that referenced
this pull request
Aug 15, 2025
…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.
jkotas
approved these changes
Aug 15, 2025
MichalStrehovsky
added a commit
that referenced
this pull request
Aug 15, 2025
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
(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