Skip to content

Improve BigInteger operators +, - and * for trivial cases - #84733

Merged
adamsitnik merged 4 commits into
dotnet:mainfrom
speshuric:addsubshortcuttrivial
Jul 7, 2023
Merged

Improve BigInteger operators +, - and * for trivial cases#84733
adamsitnik merged 4 commits into
dotnet:mainfrom
speshuric:addsubshortcuttrivial

Conversation

@speshuric

Copy link
Copy Markdown
Contributor

Move handling of trivial cases from internal methods with span arguments to to the very beginning of public operators. This avoids creating unneeded spans, checking them to empty state and some other unneeded operations.

Does not affect time of processing non-trivial arguments.

Discussion: #84721

Move handling of trivial cases from internal methods with span
arguments to to the very beginning of public operators. This avoids
creating unneeded spans, checking them to empty state and some other
unneeded operations.
Does not affect time of processing non-trivial arguments.
Discussion: #84721
@ghostghost added area-System.Numerics community-contribution Indicates that the PR has been added by a community member labels Apr 12, 2023
@ghost

Copy link
Copy Markdown

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

Issue Details

Move handling of trivial cases from internal methods with span arguments to to the very beginning of public operators. This avoids creating unneeded spans, checking them to empty state and some other unneeded operations.

Does not affect time of processing non-trivial arguments.

Discussion: #84721

Author:speshuric
Assignees:-
Labels:

area-System.Numerics

Milestone:-

@danmoseley

Copy link
Copy Markdown
Contributor

Do existing tests hit these new paths?

@speshuric

Copy link
Copy Markdown
ContributorAuthor

Do existing tests hit these new paths?

Yes, existing tests cover new code paths.

… why. To avoid that we can make an error message out of the comment. So when it fails, it's printed.

@adamsitnikadamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've merged upstream into your fork and compared the performance against main by using these benchmarks

dotnet run -c Release -f net8.0 -- --filter "*Perf_BigInteger.Add*""*Perf_BigInteger.Subtract*""*Perf_BigInteger.Multiply*" --corerun D:\projects\runtime\artifacts\bin\testhost\net8.0-windows-Release-x64\shared\Microsoft.NETCore.App\8.0.0\corerun.exe D:\projects\forks\biginteger\artifacts\bin\testhost\net8.0-windows-Release-x64\shared\Microsoft.NETCore.App\8.0.0\corerun.exe --launchCount 3

The performance for small inputs for + and - has improved. For larger inputs the change is within the range of error.

But the numbers show that the perf has regressed for multiplication of small inputs. It's most likely an outlier caused by something temporary like code alignment. In case it's not true, our Reporting System is going to detect this regression and we are going to revert the change for *

MethodJobargumentsMeanRatioAllocated
AddPR1024,1024 bits41.20 ns1.01160 B
Addmain1024,1024 bits40.70 ns1.00160 B
SubtractPR1024,1024 bits42.149 ns0.98152 B
Subtractmain1024,1024 bits43.579 ns1.00152 B
MultiplyPR1024,1024 bits864.309 ns0.99280 B
Multiplymain1024,1024 bits869.421 ns1.00280 B
AddPR16,16 bits5.786 ns0.60-
Addmain16,16 bits9.712 ns1.00-
SubtractPR16,16 bits5.803 ns0.60-
Subtractmain16,16 bits9.674 ns1.00-
MultiplyPR16,16 bits9.762 ns1.17-
Multiplymain16,16 bits8.368 ns1.00-
AddPR65536,65536 bits1,798.512 ns0.988224 B
Addmain65536,65536 bits1,834.566 ns1.008224 B
SubtractPR65536,65536 bits1,781.835 ns0.978216 B
Subtractmain65536,65536 bits1,838.100 ns1.008216 B
MultiplyPR65536,65536 bits774,176.820 ns1.0016410 B
Multiplymain65536,65536 bits774,391.028 ns1.0016410 B

@speshuric thank you for your contribution!

Comment threadsrc/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs Outdated
@adamsitnikadamsitnik self-assigned this Jul 7, 2023
@adamsitnikadamsitnik added the tenet-performance Performance related issue label Jul 7, 2023
@adamsitnik
adamsitnik merged commit 94748a2 into dotnet:mainJul 7, 2023
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 membertenet-performancePerformance related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@speshuric@danmoseley@cincuranet@huoyaoyuan@adamsitnik