Skip to content

Optimize BigInteger.ToString for large decimal string - #112178

Merged
tannergooding merged 9 commits into
dotnet:mainfrom
kzrnm:BigIntegerStringDec
Sep 16, 2025
Merged

Optimize BigInteger.ToString for large decimal string#112178
tannergooding merged 9 commits into
dotnet:mainfrom
kzrnm:BigIntegerStringDec

Conversation

@kzrnm

@kzrnmkzrnm commented Feb 5, 2025

Copy link
Copy Markdown
Contributor

This PR is a counterpart to #55121. divide-and-conquer algorithm

Number.FormatBigInteger() can run in $D(n)log(N)$ time using the Divide and Conquer algorithm, where $D(n)$ represents the computational complexity of BigInteger division.

The computational complexity of division have been improved by #96895.


Benchmark

Code
usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Configs;usingSystem.Numerics;[DisassemblyDiagnoser][GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByMethod)]publicclassToStringTest{[Params(100,1000,10000,100000)]publicintN;BigIntegerb;[GlobalSetup]publicvoidSetup(){b=BigInteger.Parse(newstring('9',N));}[Benchmark]publicstringDecimalString()=>b.ToString();}

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
ShortRun : .NET 10.0.0 (42.42.42.42424), X64 RyuJIT AVX2
Job=ShortRun IterationCount=3 LaunchCount=1 WarmupCount=3 
MethodToolchainNMeanErrorStdDevRatioRatioSDCode SizeGen0Gen1Gen2AllocatedAlloc Ratio
DecimalString\main\corerun.exe100162.6 ns72.78 ns3.99 ns1.000.003,976 B0.0176--224 B1.00
DecimalString\pr\corerun.exe100162.2 ns80.65 ns4.42 ns1.000.053,836 B0.0176--224 B1.00
DecimalString\main\corerun.exe100010,087.1 ns1,262.45 ns69.20 ns1.000.003,801 B0.1526--2024 B1.00
DecimalString\pr\corerun.exe10006,181.8 ns877.68 ns48.11 ns0.610.013,757 B0.1602--2024 B1.00
DecimalString\main\corerun.exe100001,042,496.9 ns207,485.10 ns11,372.96 ns1.000.003,801 B---20026 B1.00
DecimalString\pr\corerun.exe10000336,682.2 ns132,151.44 ns7,243.67 ns0.320.013,758 B1.4648--20025 B1.00
DecimalString\main\corerun.exe100000100,113,761.1 ns18,888,052.58 ns1,035,317.90 ns1.000.003,800 B---200155 B1.00
DecimalString\pr\corerun.exe10000011,684,286.5 ns508,446.11 ns27,869.65 ns0.120.003,757 B46.875046.875046.8750200088 B1.00

@dotnet-policy-servicedotnet-policy-serviceBot added the community-contribution Indicates that the PR has been added by a community member label Feb 5, 2025
@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.

Comment threadsrc/libraries/System.Runtime.Numerics/src/System/Number.BigInteger.cs Outdated
Comment threadsrc/libraries/System.Runtime.Numerics/src/System/Number.BigInteger.cs Outdated

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (3)

src/libraries/System.Runtime.Numerics/src/System/Number.BigInteger.cs:797

  • [nitpick] The variable name 'digitRatio' is ambiguous. It should be renamed to something more descriptive like 'logBase2ToBase1E9Ratio'.
const double digitRatio = 1.070328873472;

src/libraries/System.Runtime.Numerics/src/System/Number.BigInteger.cs:933

  • [nitpick] The method name 'BigIntegerToBase1E9' could be more descriptive. Consider renaming it to 'ConvertBigIntegerToBase1E9'.
private static void BigIntegerToBase1E9(ReadOnlySpan<uint> bits, Span<uint> base1E9Buffer, out int leadingWritten)

src/libraries/System.Runtime.Numerics/tests/BigInteger/BigIntegerToStringTests.cs:13

  • The variable 'result' is initialized to 'null' but is not explicitly set in the 'catch' block, which could lead to a 'NullReferenceException'. Ensure 'result' is properly handled in the 'catch' block.
string result = null;

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Numericscommunity-contributionIndicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@kzrnm@EgorBo@huoyaoyuan@tannergooding@jeffhandley