System.Text.Encoding
CoreLib types served from the framework’s IL: their 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.Encoding and its family: UTF8Encoding, UnicodeEncoding, UTF32Encoding, ASCIIEncoding, UTF7Encoding, Latin-1, Encoder, Decoder, the replacement and exception fallbacks, EncodingProvider, EncodingInfo, System.Text.Unicode.Utf8 (src/Csharpc/upstream-types/System.Text.Encoding.txt, CoreLib: the slicer route; #423) |
The 98 samples that encode, decode or transcode text (UTF-8/16/32, ASCII, Latin-1, code pages through EncodingProvider, fallbacks, streaming decoders, StreamReader/StreamWriter, files, HTTP, JSON, XML) byte-identical to real .NET at -O0 and -O2 within the 146-sample gate (296 runs), the transcoding ones via IL (48 with the Ascii flip) and on wasm32 (19). 545 (fallbacks across decoder chunks) caught the ldind miscompilation below before the merge |
encoding_throughput 246 912 to 325 152 B (+78 KB, stripped: the framework’s transcoders, both vector and scalar paths, and its fallback machinery) |
8.6 to 13.2 s user | median of 7: 2.038 to 0.650 s (3.1x faster); 7 775 M to 2 181 M instructions (callgrind). The price is compile time: 8.6 to 13.2 s user (+53 %), the framework’s transcoders (vector and scalar paths) and fallback machinery being more code to lower than the old copy; accepted for the run-time win. The old copy decoded scalar by scalar with an 8-at-a-time C loop for ASCII runs; the framework’s Utf8Utility/Latin1Utility/Ascii transcoders run on csharpc’s Vector128 lowering |
3 454 (corelib/Text/Encoding.cs, Coders.cs, EncodingFallbacks.cs, EncodingProvider.cs, Latin1BestFit.cs, Utf8Transcoding.cs, TranscodingStream.cs); 24 added (contract members: string.CompareOrdinal(ReadOnlySpan<char>, ReadOnlySpan<char>), string.CreateStringFromEncoding, MemoryMarshal.GetNonNullPinnableReference) |
Flipped (#423). Security: decoding of untrusted bytes is CoreLib’s own validation (maximal-subpart replacement with U+FFFD, overlong forms, surrogates and values above U+10FFFF rejected) and fallback machinery, so DecoderExceptionFallback/EncoderExceptionFallback throw exactly where .NET throws and replacement fallbacks substitute exactly what .NET substitutes, across chunk boundaries of a streaming Decoder. Lengths and counts are checked for overflow by the framework (ThrowConversionOverflow). UTF-7 stays behind System.Text.Encoding.EnableUnsafeUTF7Encoding (the slicer now keeps [FeatureSwitchDefinition], and a runtimeconfig value folds the switch as ILCompiler does). Found on the way: ldind.i1/ldind.i2 through a pointer of the other signedness zero-extended (*(sbyte*)p with a byte* read 0xE2 as 226), so Utf8.IsValid accepted a lone lead byte: a silent IL front-end miscompilation on main, fixed in general (sample 1403, #432). |
encoding_throughput: 337 384 / 1 349 400 B; compile cpu 7.4 / 10.7 s; run 224 / 343 ms (0.65x: faster than NativeAOT); 2 181 M / 2 648 M instructions |