Skip to content

Conversation

kzrnm
Copy link
Contributor

@kzrnm kzrnm commented Feb 11, 2025

Maybe fix #111708

After investigating the unexpected behavior in #111708, I concluded that JIT optimization is not functioning properly.

By splitting the part of FormatBigInteger that converts bits into base 1,000,000,000 into separate methods, I hope to improve JIT optimization.

This pull request conflicts with my pull request #112178, but I created this as I expect this PR to be backported to .NET 9.

Benchmark

In benchmarks using the released System.Runtime.Numerics.dll, it is about twice as slow compared to \main\corerun.exe.

Code
[MemoryDiagnoser(false)]
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")]
public class ToStringTest
{
    [Params([
        1<<15,
        1<<20,
        1<<21,
    ])]
    public int N;
    private char[] _dest = new char[1_000_000];

    [GlobalSetup]
    public void Setup()
    {
        bigInteger = System.Numerics.BigInteger.One << N;
    }

    BigInteger bigInteger;
    [Benchmark(Baseline = true)]
    public void TryFormat() => bigInteger.TryFormat(_dest, out _);
}

BenchmarkDotNet v0.13.12, Windows 11 (10.0.26100.3037)
13th Gen Intel Core i5-13500, 1 CPU, 20 logical and 14 physical cores
.NET SDK 10.0.100-alpha.1.25077.2
  [Host]    : .NET 10.0.0 (10.0.25.7313), X64 RyuJIT AVX2
  MediumRun : .NET 10.0.0 (42.42.42.42424), X64 RyuJIT AVX2

Job=MediumRun  IterationCount=15  LaunchCount=2  
WarmupCount=10  

Method Toolchain N Mean Ratio Allocated Alloc Ratio
TryFormat \main\corerun.exe 32768 985.8 μs 1.00 - NA
TryFormat \pr\corerun.exe 32768 984.6 μs 1.00 - NA
TryFormat \main\corerun.exe 1048576 1,008,261.6 μs 1.00 - NA
TryFormat \pr\corerun.exe 1048576 862,085.5 μs 0.86 - NA
TryFormat \main\corerun.exe 2097152 3,957,211.1 μs 1.00 - NA
TryFormat \pr\corerun.exe 2097152 3,518,594.2 μs 0.89 - NA

@kzrnm
Copy link
Contributor Author

kzrnm commented Feb 27, 2025

It seems that this might not be the appropriate change.

@kzrnm kzrnm closed this Feb 27, 2025
@kzrnm kzrnm deleted the BigIntegerToBase1E9 branch March 7, 2025 01:30
@github-actions github-actions bot locked and limited conversation to collaborators Apr 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Numerics community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Perf] BigInteger formatting performance regression in .NET 9
1 participant