Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split FormatBigInteger into smaller parts. #112413

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Feb 11, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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