Skip to content

Use Math.BigMul instead of UInt128.Multiply in BigInteger.Parse#126274

Open
kzrnm wants to merge 1 commit intodotnet:mainfrom
kzrnm:BigIntegerNumberMultiply
Open

Use Math.BigMul instead of UInt128.Multiply in BigInteger.Parse#126274
kzrnm wants to merge 1 commit intodotnet:mainfrom
kzrnm:BigIntegerNumberMultiply

Conversation

@kzrnm
Copy link
Copy Markdown
Contributor

@kzrnm kzrnm commented Mar 29, 2026

UInt128 multiplication includes some unnecessary overhead, so it's better to use Math.BigMul directly.

This PR is a refactored version of #112632.

Benchmark


BenchmarkDotNet v0.16.0-nightly.20260320.467, Windows 11 (10.0.26200.8039/25H2/2025Update/HudsonValley2)
13th Gen Intel Core i5-13500 2.50GHz, 1 CPU, 20 logical and 14 physical cores
Memory: 31.75 GB Total, 11.22 GB Available
.NET SDK 11.0.100-preview.3.26170.106
  [Host]     : .NET 11.0.0 (11.0.0-preview.3.26170.106, 11.0.26.17106), X64 RyuJIT x86-64-v3
  Job-ZICBXN : .NET 11.0.0 (11.0.0-dev, 42.42.42.42424), X64 RyuJIT x86-64-v3
  Job-QENHHP : .NET 11.0.0 (11.0.0-dev, 42.42.42.42424), X64 RyuJIT x86-64-v3


Method Toolchain N Mean Ratio Code Size Gen0 Allocated Alloc Ratio
PowerOfTenMinusOne \main\corerun.exe 1000 4.831 μs 1.00 3,285 B 0.0305 440 B 1.00
PowerOfTenMinusOne \pr\corerun.exe 1000 3.852 μs 0.80 3,284 B 0.0305 440 B 1.00
PowerOfTenMinusOne \main\corerun.exe 10000 114.386 μs 1.00 3,040 B 0.2441 4184 B 1.00
PowerOfTenMinusOne \pr\corerun.exe 10000 109.206 μs 0.95 3,040 B 0.2441 4184 B 1.00

<details>

<summary>benchmark code</summary>

```cs
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Configs;
using System.Numerics;
using System.Runtime.InteropServices;

[DisassemblyDiagnoser]
[MemoryDiagnoser(false)]
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByMethod)]
public class BigIntParseTest
{
    [Params(1000, 10000)]
    public int N;

    string s1;

    [GlobalSetup]
    public void Setup()
    {
        s1 = new string('9', N);
    }

    [Benchmark] public BigInteger PowerOfTenMinusOne() => BigInteger.Parse(s1);
}

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 29, 2026
@dotnet-policy-service
Copy link
Copy Markdown
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.

1 participant