-
Notifications
You must be signed in to change notification settings - Fork 5k
Use non-generic Array.Sort in EnumInfo on nativeaot #79473
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
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
I personally would like to take the size regression rather than the unnecessary boxing overhead (it's important for game development) |
The boxing here is one time, in fact I feel like having to JIT multiple times would take more than boxing. Not sure about the AreSorted checks though. |
NativeAOT won't JIT at runtime. |
This does not actual help much (HelloWorld before this change 2,904,576, after this change 2,897,920). The non-generic Array.Sort is special-cased for primitive types
I have tried the following:
It brought the size down to 2,819,072. The nativeaot reflection stack reads the enum values as underlying value boxes first. We can save the potential boxing and avoid some of the code duplication by sorting the enum values in the reflection stack while we have the boxes around, pass it as object[] to EnumInfo constructor like what it was done before this change, and the EnumInfo constructor can just assert that the values are sorted. The best solution for the sort footprint problem would be to sort the values at aot compilation time. It would make the sort go away completely. |
For reference, this is my workflow for using local native AOT builds:
|
We can close this then and I can revisit in January, unless you'd like to take it over before then. |
I can take it over. |
Those generic specializations won't actually be used here, though, as this check won't be met:
I assume the nativeaot compiler can't see that? |
Right. Our codegen does not perform global optimizations like this today. The codegen can only see things like this through aggressive inlining that is very limited. |
Thanks. I know that's true with the JIT; I incorrectly thought we did more whole-program optimization like that with nativeaot. |
0fd5d17
to
37fe7e9
Compare
src/libraries/System.Private.CoreLib/src/System/Enum.EnumInfo.cs
Outdated
Show resolved
Hide resolved
@MichalStrehovsky PTLA This fixes some of the size regression by reverting to non-specialized sort, similar to what we have used before. |
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.
Thank you!
For the "sort in the compiler TODO" - we would have to do this by sorting the fields themselves. Do we have any compat concerns around doing that? .NET Native used to come up with random order of metadata and I do recall it being a problem (not for enums, but in general).
Good point. We have fixed reflection to return |
...ystem.Private.Reflection.Execution/src/Internal/Reflection/Execution/NativeFormatEnumInfo.cs
Outdated
Show resolved
Hide resolved
…Internal/Reflection/Execution/NativeFormatEnumInfo.cs
Contributes to #79437
@jkotas, worthwhile? We'll pay boxing costs as part of the sort, but it should be only once per enum and only if the values aren't already sorted.