csharpc

System.Globalization

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)
Cultures and culture data (CultureInfo, CultureData, RegionInfo, CultureNotFoundException), NumberFormatInfo, casing (TextInfo), collation (CompareInfo, SortKey, SortVersion), CharUnicodeInfo, StringInfo/TextElementEnumerator, IdnMapping, DateTimeFormatInfo, every calendar (the lunisolar ones are new), and CoreLib’s DateTimeFormat, DateTimeParse, TimeSpanFormat and TimeSpanParse; DateOnly’s, TimeOnly’s and TimeSpan’s Parse/TryParse members hosted from their IL (src/Csharpc/upstream-types/System.Globalization.txt, #426) 129 samples that use cultures, culture data, casing, collation, IDN, text elements, date and time formatting and parsing, number formatting, composite formatting and text writers byte-identical to real .NET at -O0 and -O2 (258 runs) and via IL (129), 12 of them on wasm32 (the others need ICU or threads); new samples 1381 (a constrained IEquatable<T>.Equals on Vector128<T>: an explicit diagnostic before) and 1382 (generic shifts on byte/sbyte/short/ushort: invalid IR before, from C# and from IL); tests/slicer.sh 10 of 10. 369/371/372/608 (the current culture flowing into threads, tasks and the thread pool) pass through the AsyncLocal contract below a program that formats a number carries CoreLib’s culture initialization (culture data over ICU, its locale tables, the culture caches): linux-x86_64 numbers 156 672 B budget to 236 328 B measured, collections 255 944, async 270 528, linq 341 288 (budgets raised with these numbers; hello, exceptions, typeinfo unchanged). CultureInfo.GetFormat’s DateTimeFormatInfo case through the runtime trigger and composite formatting without a type switch took about 470 KB of that back. The throughput program 230 216 to 265 648 B (NativeAOT 1 378 112) throughput program 8.4 to 8.0 s (NativeAOT publish 12.9 s) throughput program (tools/il-first/bench/number_format_parse_throughput.cs: int, long, double, decimal format and parse, invariant and de-DE, composite formatting), median of 7: 1 359 to 1 323 ms; 12.48 G to 12.43 G instructions 36 966 deleted (the 52 files of corelib/Globalization the slice replaces, runtime/csrt.c’s DateTimeFormatInfo trigger), 610 added (the ICU forwarding class, the DateTime/DateTimeOffset accessors, contract members) Flipped. The ICU boundary stays the corelib’s: the slicer’s csharpc profile points CoreLib’s 29 Interop.Globalization calls at corelib/UpstreamGlobalization.cs, which forwards each to the corelib’s imports of the runtime’s ICU shim (src/runtime/globalization/pal_*.c), and keeps GlobalizationMode (invariant mode compiled from the project, ICU loaded on first use, WASI always invariant), CalendarId, CalendarDataType and TimeZoneInfoOptions the corelib’s. Interop.CallStringMethod (CalendarData’s string helper) is hosted from IL. Windows: the Windows CoreLib carries the NLS back end next to ICU’s; csharpc, like .NET by default, uses ICU, so the slicer folds GlobalizationMode.UseNls to false (the emitted IL loads the constant, the branches it rules out are not lowered, and the members and nested types only they use are not cut into the slice), and points the Win32 calls .NET still makes in ICU mode (GetLocaleInfoEx for the locale check, user overrides and code pages, GetCalendarInfoEx, GetUserGeoID/GetGeoInfo, GetUserPreferredUILanguages, LCIDToLocaleName, MultiByteToWideChar, and the Hijri calendar’s AddHijriDate registry value) at the corelib’s own imports (corelib/UpstreamWindows.cs). WASI (no ICU, invariant mode): linguistic comparisons with no option or IgnoreCase keep the runtime’s approximation of ICU’s root collation, as before the flip, through the 32-bit slice’s replacement of CompareInfo.Compare over spans (--wasi, corelib/UpstreamWasiCollation.cs). Security: every buffer handed to the shim is bounded by the managed buffer it comes from: pointer arguments are CoreLib’s own stackalloc or pinned buffers with their own lengths, and where a length travels next to an array or span (GetLocales, GetCalendars, ToAscii, ToUnicode) the forwarder clamps it to that array’s or span’s length, so a wrong length never lets native code write past it; strings go in NUL-terminated. The shim’s EnumCalendarInfo callback is CoreLib’s [UnmanagedCallersOnly] method (the slicer now copies that attribute). Compiler pieces it needed, all general: generic [UnsafeAccessor] static methods and accessors to internal types named by string; [ShadowConstructor] factories for internal value-type constructors (DateTime(long, DateTimeKind, bool)); constrained generic-math operator and IEquatable<T> calls on Vector128<T> (sample 1381); m_value, _dateData and _ticks as built-in private fields; hosted instance members of built-in value types (the slicer’s this. host roots). number_format_parse_throughput: 265 648 / 1 378 112 B; run 1 323 / 1 260 ms (1.05x; measure-aot.sh on the same host: 1 430 / 1 260 ms, 1.14x); 12.43 G / 10.54 G instructions. Number formatting itself is still the corelib’s here (#426 continues with System.Number from IL)
Number formatting and parsing: CoreLib’s System.Number with NumberFormatInfo, and the primitives’ own ToString/TryFormat/Parse/TryParse (integers, Int128/UInt128, Half, float, double, decimal) hosted from their IL (this. roots in src/Csharpc/upstream-types/System.Globalization.txt, #426); Enum.Parse in C# over CoreLib’s integer parser 206 samples that format or parse numbers, enums, cultures and composite text byte-identical to real .NET at -O0 and -O2 (412 runs), 21 via IL (459, 642, 643 among them), 21 on wasm32; tests/slicer.sh 10 of 10; conformance formatting 5 of 5 and 7 of 7. New samples 1396 (constrained ISpanFormattable/IUtf8SpanFormattable calls on built-in numbers reached their boxed slot and recursed until the stack ran out; now they call the hosted member) and 1450 (Enum.Parse edges: NBSP trimming, overflow as OverflowException, names differing only by case) the framework’s formatter and parsers replace the corelib’s smaller copies: linux-x86_64 numbers 236 024 to 254 088 B (+18 064), async 266 120 to 284 248, collections 255 640 to 269 608, linq 341 032 to 358 744, net 330 592 to 369 904, json 1 661 648 to 1 706 000, jsonwriter 832 112 to 861 464, regex 924 224 to 937 568, regexoptions 2 122 264 to 2 151 576; hello, exceptions, typeinfo unchanged (budgets raised with these numbers: a measured speed gain). The throughput program 269 832 to 337 968 B (NativeAOT 1 378 112) throughput program 6.6 to 7.2 s wall (measure-aot.sh: 9.0 s cpu, 280 MB peak; NativeAOT publish 17.7 s cpu, 216 MB) throughput program (tools/il-first/bench/number_format_parse_throughput.cs), median of 7 on the same host: 1 388 to 1 026 ms; 12.53 G to 8.53 G instructions (-32 %) 2 895 deleted, 299 added (Number.Parsing.Culture.cs, NumberParsingMembers.cs and most of Number.Formatting.cs, Number.Parsing.cs, NumberTryFormat.cs, Int128Parsing.cs; runtime/parse.c; the slicer’s NumberBridge emitter) Flipped. The corelib’s Number class keeps only the compiler’s entry points (concatenation, interpolation holes, boxed values, Console.Write), which call CoreLib’s System.Number through corelib/NumberUpstream.cs ([UnsafeAccessor], docs/12-corelib.md); the shortest-digits bridge is gone. Still the corelib’s: BigInteger’s standard formats, decimal to and from double, Math.Round(double, digits, mode), and TimeSpan’s own formatter (remaining-flips.md). Security: parsing is CoreLib’s span-based code, bounded by the input span’s length, with overflow reported as OverflowException (Parse) or false (TryParse) and never wrapped; formatting writes through CoreLib’s ValueListBuilder/span writers, so a TryFormat into a too-small destination returns false with nothing written past it (1396); NaN, infinity and sign texts come from the NumberFormatInfo in use. Enum.Parse no longer runs the C runtime’s own string parser (parse.c, deleted): the name lookup and the integer fallback are bounds-checked C#. decimal.GetBits/TryGetBits/new decimal(int[] or ReadOnlySpan<int>) stay the representation’s members (corelib/DecimalBits.cs) and validate length and scale as .NET does. Compiler pieces it needed, general: constrained interface calls on built-in value types reach the hosted IL member; ordinal comparison variants bind only non-private members (#443). number_format_parse_throughput: 338 056 / 1 378 112 B; compile cpu 9.0 / 17.7 s; run 1 025 / 1 202 ms (0.85x), was 1 323 / 1 260 ms (1.05x) with the corelib’s formatter; 8.53 G / 10.54 G instructions, was 12.43 G / 10.54 G