Repository navigation
Description
Description
After reading Performance Improvements in .NET 9 related to BigInteger, I've noticed a comment indicating that C# has poor performance compared to other programming languages in terms of big integer calculations. After quick investigation, it turned out that half of the time in https://github.com/hanabi1224/Programming-Language-Benchmarks/blob/main/bench/algorithm/edigits/1.cs is spent on formatting of a big integer. On .NET 8 it's done twice as fast, even though there were performance optimizations made in .NET 9.
Here's a benchmark code that shows the issue:
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Numerics;
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
[MemoryDiagnoser(false)]
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")]
public class Tests
{
private char[] _dest = new char[1_000_000];
private BigInteger _value =
BigInteger.Pow(BigInteger.Parse(string.Concat(Enumerable.Repeat("1234567890", 30))), 1_000);
[Benchmark(Baseline = true)]
public string Stringify() => _value.ToString();
[Benchmark]
public bool TryFormat() => _value.TryFormat(_dest, out _);
}Configuration
BenchmarkDotNet v0.14.0, Windows 11 (10.0.26100.2894)
AMD Ryzen 9 4900H with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores
.NET SDK 9.0.102
[Host] : .NET 8.0.12 (8.0.1224.60305), X64 RyuJIT AVX2
Job-TESATW : .NET 8.0.12 (8.0.1224.60305), X64 RyuJIT AVX2
Job-VBXNCI : .NET 9.0.1 (9.0.124.61010), X64 RyuJIT AVX2
Regression?
Yes. The same code on .NET 8 executes ~3 times faster (but with much more memory being allocated).
Data
| Method | Runtime | Mean | Ratio | Allocated | Alloc Ratio |
|---------- |--------- |--------:|------:|----------:|------------:|
| Stringify | .NET 8.0 | 1.009 s | 1.00 | 1303.9 KB | 1.000 |
| TryFormat | .NET 8.0 | 1.005 s | 1.00 | 719.71 KB | 0.552 |
| Stringify | .NET 9.0 | 3.657 s | 3.62 | 585.23 KB | 0.449 |
| TryFormat | .NET 9.0 | 3.657 s | 3.63 | 1.05 KB | 0.001 |
Analysis
This regression might be caused by the #100181, but I haven't thoroughly tested that regression on older codebase.
The regression is reproduced on really big integers (order of magnitude 1000+).
Activity
xakep139 commentedon Jan 22, 2025
@stephentoub FYI
dotnet-policy-service commentedon Jan 22, 2025
Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.
EgorBo commentedon Jan 22, 2025
EgorBo commentedon Jan 22, 2025
The bot can repro the regression too,
perfpoints toMath.DivRem(..., TenPowMaxPartial)as the hottest place, but I might be wrong interpretating itEgorBo commentedon Jan 22, 2025
EgorBo commentedon Jan 22, 2025
kzrnm commentedon Feb 6, 2025
kzrnm commentedon Feb 10, 2025
kzrnm commentedon Mar 3, 2025
The regression has been occurring since #106360. The DLL right after the build has no issues, but the DLL after crossgen seems to have a regression.
The size of
System.Runtime.Numerics.dllafter crossgen is different.@EgorBo
Reproduce
The
performance-simpledirectory contains benchmark projects.kzrnm commentedon Mar 5, 2025
kzrnm commentedon Mar 5, 2025
I investigated the JIT differences between d45f8b5 and 380898a using the assembly mentioned above.
Additionally, I tested a version of 380898a with
[MethodImpl(MethodImplOptions.AggressiveOptimization)]applied toFormatBigInteger.As a result, I found differences between d45f8b5 and 380898a, but the version of 380898a with
AggressiveOptimizationbehaved equivalently to d45f8b5.JIT Summary
.\coreruns\380898a3ce4-crossgen\corerun.exe
.\coreruns\380898a3ce4-crossgen-withAO\corerun.exe
.\coreruns\d45f8b5bcc5-crossgen\corerun.exe
crossgen with System.Memory
Running
crossgenincludingSystem.Memory.dllcauses a regression even in d45f8b5.JIT Summary with System.Memory
.\coreruns\380898a3ce4-crossgen\corerun.exe
.\coreruns\380898a3ce4-crossgen-withAO\corerun.exe
.\coreruns\d45f8b5bcc5-crossgen\corerun.exe