Skip to content

Reduce NativeAOT size of System.Text.Json object contracts - #137565

Draft
agocke wants to merge 2 commits into
dotnet:mainfrom
agocke:stj-aot-factory-inlining
Draft

agocke wants to merge 2 commits into
dotnet:mainfrom
agocke:stj-aot-factory-inlining

Conversation

@agocke

@agocke agocke commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Summary

Mark the existing JsonMetadataServices.CreateObjectInfo<T> -> object CreateCore<T> -> GetConverter<T> chain with AggressiveInlining. This pushes constructor and serialization-handler choices up the call graph so native aot can remove unused converter strategies and their dependencies.

Add a standalone nativeaot/SmokeTests/JsonSerialization test using ordinary source generation with one class and 16 distinct struct instantiations. It verifies exact JSON and deserialization, disables reflection-based serialization, and checks executable size for Release, size-optimized, unsanitized, single-module Linux x64 builds. Other configurations retain the functional checks without applying an uncalibrated size budget.

Size measurements

The committed smoke fixture demonstrates regression detection:

Smoke fixture Executable bytes Result against 2,764,800-byte ceiling
Three inlining annotations removed 2,839,472 Fails the size check after completing the JSON checks
Three inlining annotations present 2,634,336 Passes

Supplemental synthetic fixtures use a single shared context:

Workload Before, bytes After, bytes Saved, bytes Reduction
64 parameterless classes 2,864,080 2,801,824 62,256 2.17%
16 parameterless structs 2,912,640 2,715,584 197,056 6.77%
16 parameterized classes 2,457,264 2,449,072 8,192 0.33%
16 parameterized structs 2,908,064 2,879,392 28,672 0.99%
64 parameterless classes plus one parameterized record 2,876,592 2,855,600 20,992 0.73%
16 parameterless structs plus one parameterized record 2,953,888 2,798,112 155,776 5.27%
64 classes, metadata-only generation 2,826,704 2,760,352 66,352 2.35%
16 structs, metadata-only generation 2,900,224 2,694,976 205,248 7.08%

Sizoscope attribution confirms unused parameterized-constructor converter members disappear for the parameterless fixtures and remain for parameterized-constructor controls. The mixed cases retain shared constructor infrastructure, reducing the fixed saving; value-type specialization accounts for the larger struct savings. These are synthetic size measurements, not expected percentages for every application.

Note

The code and this description were prepared with GitHub Copilot.

agocke and others added 2 commits November 14, 2026 06:18
Keep the existing CreateObjectInfo/CreateCore/GetConverter selection chain
visible to generated callers so NativeAOT can remove unused object converter
strategies. No public API or generated-code changes.

On .NET SDK 12.0.100-alpha.1.26557.102, synthetic read/write fixtures shrink
by 62,256 bytes for 64 parameterless classes and 197,056 bytes for 16
parameterless structs. Parameterized-constructor controls also shrink.
Sizoscope confirms unused parameterized converter members are removed.

Validated all five shipping target frameworks and five STJ test suites:
67,138 passed, nine existing skips, no failures. All 12 NativeAOT fixture
runs passed. BenchmarkDotNet ShortRun timings are inconclusive; steady-state
allocations are unchanged and struct metadata creation allocates 88 B less.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: fa5a3c33-1b32-468e-ba8b-7ebcfe008fd8
Check exact source-generated JSON serialization and deserialization for a
class and 16 value-type instantiations without reflection fallback. Apply
a calibrated executable-size ceiling only to optimized, single-module,
unsanitized Linux x64 Release builds.

The native test passes at 2,634,336 bytes. Removing the three metadata
factory inlining annotations raises it to 2,839,472 bytes and fails the
same 2,764,800-byte ceiling. Both builds use identical fixture/compiler
binaries and differ only in STJ among compiler references.

Validated with the repository NativeAOT build/generated launcher and
scoped whitespace formatting checks.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: fa5a3c33-1b32-468e-ba8b-7ebcfe008fd8
@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: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

@eiriktsarpalis

Copy link
Copy Markdown
Member

You might want to consider applying the same technique to the various factory methods that are being output by the source generator.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants