csharpc

System.Text.Json

A library or CoreLib type served from the framework’s IL: its row of the per-library table of ADR 0023 (columns explained there, under “Measurements”).

Library (switch) Correctness (samples from IL) Binary size, IL / shadow Compile cpu, IL / shadow Speed, IL / shadow (time) Lines removed on a flip Decision NativeAOT on the same host, csharpc / NativeAOT (tools/il-first/measure-aot.sh: stripped size; compile cpu; run, median of 7; instructions)
System.Text.Json + System.Text.Encodings.Web 45 of 45 samples byte-identical to real .NET at -O0, at -O2 and --via-il, with no CSHARPC_IL_FIRST (the JSON samples of the A/B, the reflection-roots samples 1294 to 1302, and the encoder and buffer-writer samples 512, 607, 848, 865 and 1281); anonymous and file-local types serialize now (323 was a diagnostic of the shadow; 535 pinned the same gap and is deleted) writer + JsonDocument program (tests/size/jsonwriter.cs) 275 352 to 953 456 B (3.5x); reflection serializer program (tests/size/json.cs) 369 432 to 6 656 792 B (18.0x: the reflection resolver, the converters it reaches through MakeGenericType, the instantiations the trimmer rule roots and the attributes it reads are compiled in, where the shadow had a precomputed table); the throughput program 292 216 to 923 776 B (3.2x, measured before the Dictionary/HashSet flip merged); programs that do not use System.Text.Json are unchanged (hello 28 536, linq 170 424, collections 143 400, numbers 129 048, exceptions 36 920, typeinfo 43 776) compile cpu 10.7 to 127.3 s for json.cs (11.9x; wall 9.7 to 75.3 s), 7.6 to 19.7 s for jsonwriter.cs (2.6x), 8.4 to 18.5 s for the throughput program (2.2x) throughput program (median of 3, start-up dominated at this size): 99 to 83 ms; the earlier 5-run A/B measured 0.67x (IL 1.5x faster) 16 614 deleted in corelib/Json (ArrayBufferWriter.cs stays: System.Memory surface) and 90 added in corelib over the series; the flip commit is 18 091 deletions and 149 insertions over 48 files (git diff --shortstat): Frontend/JsonReflectionContexts*.cs, FunctionCodeGen.JsonRoots.cs, the JSON intrinsics and their enum helpers, ModuleCodeGen.JsonEnumTypes Flipped (#286), on the owner’s decision of 2026-11-09 (accept the size, compile time and speed trade for exactness and for deleting what is not ours). The closed-world MakeGenericType is solved by a general rule (#266, #282): a call to a framework method marked [RequiresUnreferencedCode] roots the types it names and every open generic definition the framework passes to MakeGenericType is instantiated over them; nothing in it is specific to JSON. Budgets (tests/size/budgets/, linux-x86_64): json 6 856 704 B, jsonwriter 983 040 B. Partial answers, named: the framework’s own IL reads an anonymous type’s and a BCL type’s attribute lists as csharpc models them (the program’s own query keeps the explicit NotSupportedException); the non-public fields of a BCL type are hidden from the framework’s Type.GetFields (System.Text.Json only keeps non-public fields marked [JsonInclude]); Exception.Source of a thrown exception whose Source was never set is null for the framework’s IL (the serializer only compares it with its rethrow token). The size of the reflection serializer is the open lever (follow-up). Default since #320 (owner decision 2026-11-10): reflection-based JSON follows NativeAOT. A program that says nothing gets IsReflectionEnabledByDefault=false and the framework’s own InvalidOperationException; JsonSerializerIsReflectionEnabledByDefault (property, #:property, runtimeconfig) always wins and an explicit PublishAot=false leaves it on, as .NET evaluates them. [FeatureSwitchDefinition] getters fold to the configured value (general, as ILCompiler), so a reflection-JSON program that does not opt in costs 48 s of compile cpu and 544 MB peak (744 s and 3.3 GB opted in) and 2.4 MB instead of 32 MB unstripped at the default level; opted in, nothing changes (file-based apps, which had the switch off already, got this drop with #290’s folding: tests/size/json.cs 6 656 792 to 1 769 992 B; #320 gives projects and IL programs the same default). The rewrite that nulled the source generator’s AttributeProviderFactory lambdas is gone (it was the last JSON-specific code in the compiler). json_throughput: 815 672 / 1 628 688 B; compile cpu 24.8 / 20.1 s; run 75 / 68 ms (1.11x); 0.55 G / 0.48 G instructions; json: 1 558 784 / 1 989 368 B; compile cpu 35.5 / 21.5 s; run 241 / 199 ms (1.21x); 0.8 M / 5.5 M instructions; jsonwriter: 801 336 / 1 567 216 B; compile cpu 20.9 / 12.7 s; run 4 / 5 ms (0.80x); 0.5 M / 1.5 M instructions (json throws: reflection-based JSON is off by default)