Skip to content

Inline NativeAOT resource keys at resource accessor call sites - #137499

Closed
MichalStrehovsky wants to merge 1 commit into
dotnet:mainfrom
MichalStrehovsky:nativeaot-resource-key-inlining
Closed

MichalStrehovsky wants to merge 1 commit into
dotnet:mainfrom
MichalStrehovsky:nativeaot-resource-key-inlining

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Reuse the existing System.SR accessor-to-ldstr rewrite when System.Resources.UseSystemResourceKeys is enabled, emitting the resource key instead of its value while retaining resource-dictionary validation.

Measured atop the GC size fixes, stripped NativeAOT WASI HelloWorldSize decreases from 662,805 to 659,932 bytes, saving 2,873 bytes (0.43%) and eliminating all 63 SR getter bodies. Validated the compiler build, HelloWorldSize build/run (exit 100), and direct/formatted exception messages with resource keys enabled and disabled.

Reuse the existing System.SR accessor-to-ldstr rewrite when System.Resources.UseSystemResourceKeys is enabled, emitting the resource key instead of its value while retaining resource-dictionary validation.

Measured atop the GC size fixes, stripped NativeAOT WASI HelloWorldSize decreases from 662,805 to 659,932 bytes, saving 2,873 bytes (0.43%) and eliminating all 63 SR getter bodies. Validated the compiler build, HelloWorldSize build/run (exit 100), and direct/formatted exception messages with resource keys enabled and disabled.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: e1b16afd-ac3c-4144-ae84-b4a37986ab3c
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Key inlining incorrectly changes CategoryAttribute.Appearance.Category from "Appearance" to "PropertyCategoryAppearance" when resource-key mode is enabled, breaking the two-argument GetResourceString accessor’s default-text behavior.

1 open issue
What changed in this PR

Extends NativeAOT’s existing resource-accessor optimization to inline resource keys when System.Resources.UseSystemResourceKeys is enabled, reducing retained getter bodies and executable size.

Changes:

  • Selects resource keys or values after validating the resource-dictionary entry.
  • Enables call-site string-literal rewriting in resource-key mode and updates the explanatory comment.
File Description
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​SubstitutionProvider.cs Selects the key or resource value according to the feature switch.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​SubstitutedILProvider.cs Removes the guard that prevented accessor rewriting in resource-key mode.

Review effort: Balanced

&& dict.TryGetValue(resourceStringName, out string result))
{
return result;
return UseResourceKeys ? resourceStringName : result;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That explains why I left the

            // Do not attempt to inline resource strings if we only want to use resource keys.
            // The optimizations are not compatible.

comment. I did push AI to explain what problems could be. I guess PR reviews have access to better models.

@MichalStrehovsky
MichalStrehovsky deleted the nativeaot-resource-key-inlining branch November 14, 2026 06:43
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