Variable assignments in System.Random's xorshiro256ss' NextUInt64() can be removed #95116
-
In particular, these assignments doesn't have to be there, the cpu is copying bytes which I say is ~30% overhead after seeing in profiler that's how much it was spending on dry run of the function |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Simple benchmark does not support this: using System.Numerics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
public class Program
{
static void Main()
{
BenchmarkRunner.Run<Program>();
}
private ulong _s0 = 123, _s1 = 456, _s2 = 789, _s3 = 1000;
[Benchmark(Baseline = true)]
public ulong NextUInt64()
{
ulong s0 = _s0, s1 = _s1, s2 = _s2, s3 = _s3;
ulong result = BitOperations.RotateLeft(s1 * 5, 7) * 9;
ulong t = s1 << 17;
s2 ^= s0;
s3 ^= s1;
s1 ^= s2;
s0 ^= s3;
s2 ^= t;
s3 = BitOperations.RotateLeft(s3, 45);
_s0 = s0;
_s1 = s1;
_s2 = s2;
_s3 = s3;
return result;
}
[Benchmark]
public ulong NextUInt64Alt()
{
ulong result = BitOperations.RotateLeft(_s1 * 5, 7) * 9;
ulong t = _s1 << 17;
_s2 ^= _s0;
_s3 ^= _s1;
_s1 ^= _s2;
_s0 ^= _s3;
_s2 ^= t;
_s3 = BitOperations.RotateLeft(_s3, 45);
return result;
}
} BenchmarkDotNet v0.13.7, Windows 11 (10.0.22631.2715)
|
Beta Was this translation helpful? Give feedback.
-
Actually it's the opposite, by assigning those fields to locals the JIT can assume no other code can alter the values, which can result in better codegen. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Simple benchmark does not support this: