Closed
Description
Description
After migrating a genetic algorithm project to .NET 9, I noticed that there was a performance regression, and I was able to track it down to a single method that performs inverse mutation of the given chromosome (in my case it is just an array of ints). I tried many different seeds, and the result is the same: about 10-15% regression. I also set <CETCompat>false</CETCompat>
in the .csproj, but it did not help.
The problem is that this method is on a hot path, and eventually this adds up to milliseconds of difference between .NET 8 and .NET 9.
Benchmark code:
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<Benchmarks>();
[MemoryDiagnoser, DisassemblyDiagnoser, SimpleJob(RuntimeMoniker.Net80), SimpleJob(RuntimeMoniker.Net90)]
public class Benchmarks
{
private const int Seed = 100;
private static readonly int[] Chromosome = new int[59];
private static readonly Random Random = new(Seed);
public Benchmarks()
{
var genes = Enumerable.Range(1, 59).ToArray();
Random.Shuffle(genes);
genes.CopyTo(Chromosome.AsSpan());
}
[Benchmark]
public int[] InversionMutation()
{
var idx1 = 0;
var idx2 = 0;
var chromosomeLength = Chromosome.Length;
while (idx1 == idx2)
{
idx1 = Random.Next(chromosomeLength);
idx2 = Random.Next(chromosomeLength);
}
if (idx1 > idx2)
{
(idx1, idx2) = (idx2, idx1);
}
Array.Reverse(Chromosome, idx1, idx2 - idx1 + 1);
return Chromosome;
}
}
Regression?
Yes
Data
BenchmarkDotNet v0.14.0, Windows 11 (10.0.22631.4169/23H2/2023Update/SunValley3)
AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
.NET SDK 9.0.100-rc.2.24468.2
[Host] : .NET 8.0.8 (8.0.824.36612), X64 RyuJIT AVX2
.NET 8.0 : .NET 8.0.8 (8.0.824.36612), X64 RyuJIT AVX2
.NET 9.0 : .NET 9.0.0 (9.0.24.46307), X64 RyuJIT AVX2
Method | Job | Runtime | Mean | Error | StdDev | Code Size | Allocated |
---|---|---|---|---|---|---|---|
InversionMutation | .NET 8.0 | .NET 8.0 | 27.10 ns | 0.020 ns | 0.018 ns | 794 B | - |
InversionMutation | .NET 9.0 | .NET 9.0 | 31.14 ns | 0.039 ns | 0.033 ns | 914 B | - |