csharpc

System.Random

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.Random (src/Csharpc/upstream-types/System.Random.txt, a CoreLib type: the slicer route of List<T>) the seeded samples (198, 964) and a new unseeded one (1319: ranges, Random.Shared, NextBytes, Shuffle, GetItems, the seeded sequence next to them) byte-identical to real .NET at -O0 and -O2; the bank program at -O0 and -O2 198 238 144 to 293 320 B (1.23x), 1319 172 912 to 221 928 B (1.28x) compile cpu 8.1 to 7.8 s (198), 6.6 to 9.5 s (1319) a throughput program (50 M Next(1000) on a seeded and an unseeded generator, 50 MB of NextBytes, 20 M NextDouble): median of 5, 0.81 to 0.85 s (1.05x, within the noise of the loaded VPS) 1 551 (corelib/Random/Random*.cs; CoreDebug.cs stays, Guid and RandomNumberGenerator use it) Flipped (#307). Security: the default seed comes from the OS RNG as in .NET: the slice’s Interop.GetRandomBytes (libSystem.Native upstream) is replaced by the runtime’s csrt_os_random (getentropy, rand_s, WASI random_get; #292), which seeds string hashing too, and the program stops if the OS cannot provide entropy (CoreCLR fails fast too). The generators are upstream’s code (xoshiro256** and the legacy seeded one). Also: BitOperations.Log2Ceiling (exact, upstream’s), and double.ConvertToIntegerNative, exact in range and an explicit NotSupportedException for NaN and out-of-range values, whose result is the platform instruction’s (the legacy generator only converts in range). The slicer forwards a call to a stopped type’s member with a replacement (Interop.GetRandomBytes) to csharpc’s own. random_throughput: 155 136 / 1 341 192 B; compile cpu 6.5 / 18.1 s; run 815 / 550 ms (1.48x); 8.50 G / 6.05 G instructions. After AggressiveInlining is honoured and the argument checks and ConvertToIntegerNative keep their throws out of line (#351): 7.15 G / 6.05 G instructions (1.18x, was 1.41x), run 1 546 / 1 279 ms (1.21x; wall times on the loaded VPS vary by 1.8x between runs, instructions are the steady measure). The rest of the gap is the indirect call to Random.Next(int) (Random.Shared’s ThreadSafeRandom overrides it, so CHA cannot bind it): #370