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

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

Merged
merged 4 commits into from
Jul 7, 2023

Conversation

speshuric
Copy link
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: dotnet#84721
@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Apr 12, 2023
@ghost
Copy link

ghost commented Apr 12, 2023

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
Member

Do existing tests hit these new paths?

@speshuric
Copy link
Contributor Author

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.
Copy link
Member

@adamsitnik adamsitnik left a comment

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 *

Method Job arguments Mean Ratio Allocated
Add PR 1024,1024 bits 41.20 ns 1.01 160 B
Add main 1024,1024 bits 40.70 ns 1.00 160 B
Subtract PR 1024,1024 bits 42.149 ns 0.98 152 B
Subtract main 1024,1024 bits 43.579 ns 1.00 152 B
Multiply PR 1024,1024 bits 864.309 ns 0.99 280 B
Multiply main 1024,1024 bits 869.421 ns 1.00 280 B
Add PR 16,16 bits 5.786 ns 0.60 -
Add main 16,16 bits 9.712 ns 1.00 -
Subtract PR 16,16 bits 5.803 ns 0.60 -
Subtract main 16,16 bits 9.674 ns 1.00 -
Multiply PR 16,16 bits 9.762 ns 1.17 -
Multiply main 16,16 bits 8.368 ns 1.00 -
Add PR 65536,65536 bits 1,798.512 ns 0.98 8224 B
Add main 65536,65536 bits 1,834.566 ns 1.00 8224 B
Subtract PR 65536,65536 bits 1,781.835 ns 0.97 8216 B
Subtract main 65536,65536 bits 1,838.100 ns 1.00 8216 B
Multiply PR 65536,65536 bits 774,176.820 ns 1.00 16410 B
Multiply main 65536,65536 bits 774,391.028 ns 1.00 16410 B

@speshuric thank you for your contribution!

@adamsitnik adamsitnik self-assigned this Jul 7, 2023
@adamsitnik adamsitnik added the tenet-performance Performance related issue label Jul 7, 2023
@adamsitnik adamsitnik merged commit 94748a2 into dotnet:main Jul 7, 2023
103 checks passed
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 tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants