csharpc

System.Linq

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.Linq (System.Linq) 9 of 9 samples byte-identical to real .NET in the A/B (104, 105, 132, 524, 931, 1235, 546, 552, the throughput program); after the flip also at -O0 and -O2 with the new 1310 (generic virtual methods through named-tuple type arguments) and 1311 (Vector<T>). Security: the shadow’s guards (argument checks, checked Sum/Average overflow in the order .NET’s vectorized Sum detects it, empty-sequence and out-of-range exceptions) are the framework’s own code now; checked by the exception-text samples above (931 includes the lane-order overflow cases, which pass only with Vector<T> on 16-byte vectors) and by the explicit span-length checks the new Vector<T> constructors/CopyTo/Create make (ArgumentOutOfRangeException/ArgumentException with .NET’s messages, sample 1311) 1.35x over the 9 samples (12 194 against 9 062 KB): 104_linq 300 to 896 KB, 524 186 to 235 KB, the Xml.Linq samples tie (+1 to +3 %); tests/size linq 170 424 to 253 288 B (1.49x), async (one Sum()) 156 096 to 157 248 B; 4.2x for the throughput program (310 to 1 300 KB): every iterator class the library constructs brings all its virtual slots 1.45x (372 against 256 CPU s over 9 samples; wall 1.38x) 0.86x user CPU on the throughput program (min of 7: 1.78 against 2.07 s; wall medians on the loaded VPS 3.64 against 5.44 s) 1 875 in corelib/Enumerable.cs + LinqIterators.cs, 108 of LinqSpans.cs (the part Array.IndexOf and MemoryExtensions.Min/Max use stays as RuntimeSpans.cs), 58 of C (csrt_linq_sum_i32) Flipped (#267): correct and faster, size and compile time are the price. Blockers closed in the compiler (general, nothing Linq-specific): System.Numerics.Vector<T> is Vector128<T> (16 bytes, NativeAOT’s default; Vector.IsHardwareAccelerated follows Vector128), with construction from a span or a value, Widen, Sum, Indices, CopyTo(Span), acc += v; Vector<T>.IsSupported; IMinMaxValue<T> constants on primitives; generic virtual and generic interface dispatch erases tuple element names (a call through Iterator<(string Name, int Age)> found no case); and the framework-IL choice counts a method name several assemblies of a namespace define for the assembly named like the namespace (Select is System.Linq’s on IEnumerable<T>). T[] constructors and CopyTo(T[]) of Vector<T> stay explicit diagnostics. [FeatureSwitchDefinition] properties fold to the value the runtime configuration sets (#290). csharpc sets System.Linq.Enumerable.IsSizeOptimized exactly as .NET does for the kind of program, because the switch is observable (TryGetNonEnumeratedCount answers differently, AppContext exposes it; samples 462 and 345): single files get the file-based app’s default (true, the SDK’s PublishAot default), a project gets what its evaluation sets (true with PublishAot=true through Microsoft.NETCore.Native.targets, unset and so false without it), and UseSizeOptimizedLinq / the runtimeconfig switch / #:property always win. For information, throughput program, user CPU min of 7: speed-optimized 0.79 s, size-optimized 1.80 s (the shadow was 2.07 s); stripped 1 369 against 969 KB. Samples 1313 (false) and 1314 (true) print the same as .NET. csharpc’s own build (Csharpc.csproj, compiled by tools/bootstrap/selfhost.sh) sets UseSizeOptimizedLinq=true, which its self-compile needs to stay within the CI runner’s memory. linq_throughput: 751 704 / 1 838 336 B; compile cpu 23.3 / 19.4 s; run 3 149 / 3 141 ms (1.00x); 15.66 G / 14.69 G instructions